How to catch Spring bean creation error - Injectio

2019-08-16 21:52发布

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>

that is error code.

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)

I thought about the cause of the error, but I think it is because I did not insert the service annotation.

However, there is no typos in any way, and everything is written correctly and errors occur. Is there something I don't know?

Can you tell me what caused this error?

And what about the solution?

3条回答
看我几分像从前
2楼-- · 2019-08-16 21:57

try this to scan your Service Package and Dao Package.

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

above code will scan the dao and service package respectively.

查看更多
等我变得足够好
3楼-- · 2019-08-16 22:13

A mapper is registered to Spring by including a MapperFactoryBean in your XML config file like follows:

<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>
查看更多
姐就是有狂的资本
4楼-- · 2019-08-16 22:16

make sure that AdminDao bean is creating and injecting correctly into AdminServiceImple

use this tag in your spring-cfg.xml file

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

and also scan the controller class using --

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

you have to give information of class which are going to use annotation to IOC container to create bean...

查看更多
登录 后发表回答