如何捕获Spring bean创建错误-自动关联的注入? [英] How to catch Spring bean creation error - Injection of autowired dependency?

查看:72
本文介绍了如何捕获Spring bean创建错误-自动关联的注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AdminService.java

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

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)

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

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?

还有什么

推荐答案

尝试使用此方法扫描您的 Service 软件包并 Dao 软件包。

try this to scan your Service Package and Dao Package.

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

上面的代码将扫描 dao service 包。

above code will scan the dao and service package respectively.

这篇关于如何捕获Spring bean创建错误-自动关联的注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆