【问题标题】:How to catch Spring bean creation error?如何捕捉 Spring bean 创建错误?
【发布时间】:2017-08-06 23:58:04
【问题描述】:

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)

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

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

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

那么解决方案呢?

请帮帮我..

【问题讨论】:

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


    【解决方案1】:

    尝试更改您的此代码:

    @Autowired
    public AdminService aService;
    

    到这里:

    @Autowired
    public AdminService adminService;
    

    因为当您使用 Autowire 时,他无法在您的上下文中看到 aService,这就是为什么他无法创建 adminService 的实例作为您的 Service bean 的默认名称。

    编辑:

    另一件事是您必须将接口实现到具体类才能实例化 bean,记住您无法实例化接口,因此您需要具体的 bean 来实现接口上的内容。像这样:

    @Repository
    public class IAdminDAOImpl implements IAdminDAO {
         //Implementation here
    }
    
    @Service
    public class AdminServiceImpl implements AdminService {
        //Implementation here
    }
    

    【讨论】:

    • 我尝试按照你告诉我的修改代码来运行项目,但是也报错了。 @LKTN.25
    • 我认为您应该将 IAdminDAO 实现到像 IAdminDAOImpl 这样的具体 bean。并将@Service 放在它上面,以便上下文将其识别为spring bean,并且自动装配将找到您的实现。
    • 例如,如何? @LKTN.25
    • 现在您已经修改了代码,请检查下面的链接。 link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2010-12-06
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多