如何捕获Spring bean创建错误 - 注入自动连接依赖?

时间:2017-08-07 01:40:43

标签: spring spring-mvc dependency-injection

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)

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

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

你能告诉我导致这个错误的原因吗?

那解决方案呢?

3 个答案:

答案 0 :(得分:0)

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

在spring-cfg.xml文件中使用此标记

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

并使用 -

扫描控制器类

<context:component-scan base-package="controller" />

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

答案 1 :(得分:0)

映射器通过在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>

答案 2 :(得分:0)

尝试此操作来扫描您的bundle exec jekyll serve软件包和Service软件包。

Dao

以上代码将分别扫描<context:component-scan base-package="dao, service" /> dao包。