【问题标题】:How to catch Spring bean creation error - Injection of autowired dependency?如何捕获 Spring bean 创建错误 - 注入自动装配依赖项?
【发布时间】:2017-08-07 01:40:42
【问题描述】:

AdminService.java

    package service;

    import java.util.HashMap;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import dao.IAdminDAO;
    import dao.IMemberDAO;

    public interface AdminService
    {   
        public HashMap<String, Object> adminLogin(String id,String pw);
    }

AdminServiceImple.java
    package service;

    import java.util.HashMap;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import dao.IAdminDAO;

    @Service
    public class AdminServiceImple implements AdminService
    {

        @Autowired
         private IAdminDAO adminDao;

        @Override
        public HashMap<String, Object> adminLogin(String id, String pw) 
        {
            HashMap<String, Object> adminResult = adminDao.selectOne(id);

            if(adminResult != null)
            {
                String opwd = (String) adminResult.get("pw");

                if(opwd.equals(pw))
                {
                    if(adminResult.get("authority").equals(true))
                    {
                        return adminResult;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
    }

AdminController.java

    package controller;

    import java.io.IOException;
    import java.util.HashMap;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;

    import service.AdminService;
    import service.AdminServiceImple;
    import service.MemberService;

    @Controller
    public class AdminController 
    {
        @Autowired
        public AdminServiceImple adminService;

        // 관리자 로그인 폼 페이지
        @RequestMapping("admin.do")
        public String adminLoginPage()
        {
            return "adminLoginPage";
        }

        // 관리자 로그인했을 시 요청
        @RequestMapping("adminLoginOK.do")
        @ResponseBody
        public String adminMainPage(@RequestParam(required=false) String id, @RequestParam(required=false)String pw,HttpSession session,HttpServletRequest req,HttpServletResponse resp)
        {
            HashMap<String, Object> adminLoginIdentify = adminService.adminLogin(id, pw);

            if(adminLoginIdentify != null)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        @RequestMapping("adminPage.do")
        public String adminPage(HttpSession session,HttpServletRequest resquest,HttpServletResponse response) throws IOException
        {
            return "adminMainPage";
        }
    }

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="dao, service" />

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <property value="com.mysql.jdbc.Driver" name="driverClassName"></property>
        <property value="jdbc:mysql://localhost/rachelvf" name="url"></property>
        <property value="root" name="username"/>
        <property value="mysql" name="password"/>
    </bean>

    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations" value="classpath:dao/mapper/*.xml"></property>
        <property name="typeAliasesPackage" value="model"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>

   <bean class="org.mybatis.spring.mapper.MapperFactoryBean"  id="memberDao">
     <property name="mapperInterface" value="dao.IMemberDAO"></property> 
      <property name="sqlSessionFactory" ref="SqlSessionFactory"></property>
   </bean>

   <bean class="org.mybatis.spring.mapper.MapperFactoryBean"  id="adminDao">
     <property name="mapperInterface" value="dao.IAdminDAO"></property> 
     <property name="sqlSessionFactory" ref="SqlSessionFactory"></property>
   </bean>
</beans>

这是错误代码。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminServiceImple': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.IAdminDAO service.AdminServiceImple.adminDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.IAdminDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)

我想到了错误的原因,但我认为是因为我没有插入服务注释。

但是,无论如何都没有拼写错误,并且所有内容都正确写入并发生错误。有什么我不知道的吗?

你能告诉我是什么导致了这个错误吗?

那么解决方案呢?

【问题讨论】:

  • IAdminDAO bean 丢失... AdminDAO 是否声明为 Spring bean?

标签: spring spring-mvc dependency-injection


【解决方案1】:

确保AdminDao bean 正在创建并正确注入 AdminServiceImple

在你的 spring-cfg.xml 文件中使用这个标签

&lt;context:component-scan base-package="dao" /&gt;

并且还使用 --

扫描控制器类

&lt;context:component-scan base-package="controller" /&gt;

您必须提供将使用注解到 IOC 容器的类的信息来创建 bean...

【讨论】:

  • 我已经尝试按照您所说的添加代码,但它也会引发错误。如果我说实话,我无法理解。
  • 确保您正在扫描您将在其中使用注释的所有类,但域类(JavaBean 的)除外
  • 我在 applicationContext.xml 中添加了 。我还添加了 xmlns:mybatis-spring = "mybatis.org/schema/mybatis-spring"。
  • 结果又出现了一个错误。错误代码是 org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 14 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid;就是这样。
  • 使用这个标签代替
【解决方案2】:

通过在您的 XML 配置文件中包含 MapperFactoryBean 将映射器注册到 Spring,如下所示:

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
      <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
      <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

【讨论】:

    【解决方案3】:

    试试这个扫描你的Service 包和Dao 包。

    <context:component-scan base-package="dao, service" />
    

    上面的代码将分别扫描daoservice包。

    【讨论】:

    • 你能和我进行一对一的对话吗?
    • 很抱歉,我在我们办公室的 stackoverflow 中的聊天被阻止了。您目前的问题是什么?
    • 我按照你说的修改了代码,但是还是不行,报错。
    • 并且我修改了applicationContext.xml,但是请再次参考上面的代码。
    • 项目仍未运行。同样的错误仍然发生。由于这个错误,我无法继续进行该项目,我很遗憾。
    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2015-09-05
    • 1970-01-01
    • 2015-12-11
    相关资源
    最近更新 更多