如何捕获Spring bean创建错误?

时间:2017-08-06 23:58:05

标签: spring-mvc model-view-controller spring-security

AdminService.java

package service;

import java.awt.Window.Type;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

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


import dao.IMemberDAO;
import model.Member;

@Service
public class MemberService  
{
    @Autowired
    private IMemberDAO memberDao;

    // 로그인
    public HashMap<String, Object> login(String id,String pw)
    {
        HashMap<String, Object> result = memberDao.selectOne(id);

        if(result != null) // 존재하는 값이 있으면
        {
            String opwd = (String) result.get("pw"); // opwd = 존재하는값의 pw값을 가져온 값

            if(opwd.equals(pw)) 
            {
                return result; // true면 존재하는값을 반환
            }
            else
            {
                return null;
                //return null; // 아니면 값을 반환하지 않음.
            }
        }
        else // 존재하는 값이 없다면
        {
            return null;
        }
    }

    // 아이디 체크
    public boolean idCheck(String loginPerson)
    {
        HashMap<String, Object> user = memberDao.selectOne(loginPerson);

        if(user != null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    // 멤버 추가
    public boolean insertMember(Member member)
    {
        if(idCheck(member.getId()))
        {
            memberDao.insertMember(member);
            return true;
        }
        else
        {
            return false;
        }
    }

    public HashMap<String, Object> getMemberInfo(String id)
    {
        return memberDao.selectOne(id);
    }

    // 회원 수정
    public void memberUpdate(HashMap<String, Object> params)
    {
        System.out.println("params is : " + params);
        memberDao.updateMember(params);
    }

    // 회원삭제
    public boolean memberDelete(String id,String pw)
    {
        HashMap<String, Object> user = memberDao.selectOne(id);

        String pw2 = (String) user.get("pw");
        System.out.println("id and pw is : " + id + "/" + pw);
        System.out.println("pw2 is : " + pw2);


        if(pw2.equals(pw))
        {
            memberDao.deleteMember(id);
            System.out.println("return true");
            return true;
        }
        else
        {
            System.out.println("return false");
            return false;
        }
    }
}

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.MemberService;

@Controller
public class AdminController 
{
    @Autowired
    public AdminService aService;

    // 관리자 로그인 폼 페이지
    @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 = aService.adminLogin(id, pw);

        //if(session.getAttribute("id") == null){System.out.println("zzz kkk");}
        //String admin_id = (String) session.getAttribute("id");

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

    @RequestMapping("adminPage.do")
    public String adminPage(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue="0") int type,
            HttpSession session,HttpServletRequest resquest,HttpServletResponse response) throws IOException
    {
        ModelAndView mav = new ModelAndView();
        HashMap<String, Object> params = new HashMap<String, Object>();

        params.put("type", type);
        params.put("keyword", keyword);
        System.out.println(params);


        return "adminMainPage";
    }
}

adminDaoMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="dao.IAdminDAO">
    <select id="selectOne" parameterType="Strig" resultType="java.util.HashMap">
        select * from member where id = #{id}
    </select>

    <select id="selectMemberAll" resultType="java.util.HashMap">
        select * from member
    </select>
</mapper>

AdminDao.java

package dao;

import java.util.HashMap;

public interface IAdminDAO 
{
    public HashMap<String, Object> selectOne(String id);
}

修改applicationContext文件之前的代码如下所示。

<?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="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 id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath*:dao/mapper/*.xml"></property>
    </bean>

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

</beans>

这是修改后的应用代码。

<?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="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>
</beans>

该项目在修改applicationContext文件之前运行良好, 但在修改代码后,会发生错误。

错误代码是。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.IAdminDAO service.AdminService.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)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)

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

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

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

那解决方案呢?

请帮帮我..

1 个答案:

答案 0 :(得分:1)

尝试更改您的此代码:

@Autowired
public AdminService aService;

到此:

@Autowired
public AdminService adminService;

因为当您自动加入时,他无法在您的上下文中看到aService,这就是为什么他无法创建adminService的实例作为您的{的默认名称{1}} bean。

修改

另一件事是你必须实现你的接口到一个具体的类来实例化bean,你不能记住实例化接口,所以你需要具体的bean来实现你的接口上的东西。像这样:

Service