복붙노트

[SPRING] @Autowired - 적어도 하나의 bean에 의존성을 위해 발견 된 유형의 적격 bean이 없습니다.

SPRING

@Autowired - 적어도 하나의 bean에 의존성을 위해 발견 된 유형의 적격 bean이 없습니다.

현재 컨트롤러와 서비스 레이어 간의 Autowire 구성 문제가 있습니다.

나는 실수를 추적 할 수 없다.

간단한 로그 정보

    SEVERE:   Exception while loading the app
    SEVERE:   Undeployment failed for context /OTT
    SEVERE:   Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.ott.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

아래에서는 컨트롤러 및 서비스 계층 코드와 함께 dispatcher-servlet.xml도 제공합니다.

제어 장치

package com.ott.controller;

import com.ott.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

    /**
     *
     * @author SPAR
     */
    @Controller
    public class AdminController {

        private EmployeeService employeeService;

        @RequestMapping("/employee")
        public String employee(){
            this.employeeService.fetchAll();
            return "employee";
        }

        @Autowired(required = true)
        @Qualifier(value="employeeService")
        public void setEmployeeService(EmployeeService empService) {
            this.employeeService = empService;
        }

    }

서비스 인터페이스

package com.ott.service;

import com.ott.hibernate.Employee;
import java.util.List;

/**
 *
 * @author SPAR
 */
public interface EmployeeService {

     List<Employee> fetchAll();


}

서비스 인터페이스 Impl

package com.ott.service;

import com.ott.dao.EmployeeDAO;
import com.ott.hibernate.Employee;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author SPAR
 */
@Service
public class EmployeeServiceImpl implements EmployeeService{

    private EmployeeDAO employeeDAO;

    @Override
    @Transactional(readOnly = true)
    public List<Employee> fetchAll() {

        List<Employee> employees = employeeDAO.fetchAll();
        for (Employee employee : employees) {

            System.out.println("Name : "+employee.getFirst_Name() +" "+ employee.getLast_Name());

            System.out.println("Email Id : "+employee.getEmail_Id());
        }

        return employees;
    }

    @Autowired(required = true)
    @Qualifier(value="employeeDAO")
    public void setEmployeeDAO(EmployeeDAO empDAO) {
        this.employeeDAO = empDAO;
    }
}

Dispatcher-servlet.xml

 <?xml version='1.0' encoding='UTF-8' ?>
    <!-- was: <?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:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"       
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

        <context:component-scan base-package="com.ott.controller"/>
        <context:component-scan base-package="com.ott.hibernate"/>
        <context:component-scan base-package="com.ott.service"/>
        <context:component-scan base-package="com.ott.dao"/>

        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

        <mvc:resources mapping="/resources/**" location="/resources/" />

         <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
            <property name="definitions">
                <list>
                    <value>/WEB-INF/tiles-def/general-layout.xml</value>
                </list>
            </property>
        </bean>

        <bean id="viewResolverTiles" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
        </bean> 

        <mvc:annotation-driven />

        <bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix">
            <value>/WEB-INF/pages/</value>
          </property>
          <property name="suffix">
            <value>.jsp</value>
          </property>
        </bean>
    </beans>

해결법

  1. ==============================

    1.반드시 이름과 한정어를 제공 할 필요는 없습니다. 이름을 설정하면 bean이 컨텍스트에 등록 된 이름이됩니다. 서비스 이름을 제공하지 않으면 BeanNameGenerator를 기반으로하는 비구증의 비 정규화 클래스 이름으로 등록됩니다. 따라서 귀하의 경우 구현은 employeeServiceImpl로 등록됩니다. 그래서 그 이름을 autowire하려고하면 직접 해결해야합니다.

    반드시 이름과 한정어를 제공 할 필요는 없습니다. 이름을 설정하면 bean이 컨텍스트에 등록 된 이름이됩니다. 서비스 이름을 제공하지 않으면 BeanNameGenerator를 기반으로하는 비구증의 비 정규화 클래스 이름으로 등록됩니다. 따라서 귀하의 경우 구현은 employeeServiceImpl로 등록됩니다. 그래서 그 이름을 autowire하려고하면 직접 해결해야합니다.

    private EmployeeService employeeServiceImpl;
    
    @RequestMapping("/employee")
    public String employee() {
        this.employeeService.fetchAll();
        return "employee";
    }
    
    @Autowired(required = true)
    public void setEmployeeService(EmployeeService employeeServiceImpl) {
        this.employeeServiceImpl = employeeServiceImpl;
    }
    

    @Qualifier는 동일한 유형의 bean이 두 개 이상 존재하고 다양한 목적으로 다른 구현 bean을 autowire하려는 경우에 사용됩니다.

  2. ==============================

    2.문제를 발견 한 사람들

    문제를 발견 한 사람들

    방금 직원 서비스에 한정자 이름을 추가하여 시도해 보니 마침내 내 문제가 해결되었습니다.

    @Service("employeeService")
    
    public class EmployeeServiceImpl implements EmployeeService{
    
    }
    
  3. ==============================

    3.@Service를 사용하려면 아래와 같이 한정자 이름을 추가해야합니다.

    @Service를 사용하려면 아래와 같이 한정자 이름을 추가해야합니다.

    @Service ( "employeeService")가 문제를 해결해야합니다.

    또는 @Service 뒤에 @Qualifier annontion을 추가해야합니다.

    @Service
    @Qualifier("employeeService")
    
  4. ==============================

    4.EmployeeService 유형의 bean이 하나 뿐이며 EmployeeService 인터페이스에 다른 구현이없는 경우 Setter 메소드 앞에 EmployeeServiceImpl 및 @Autowire 앞에 "@Service"를 넣으면됩니다. 그렇지 않으면 @Service ( "myspecial")와 같은 특수 빈의 이름을 지정하고 setter 메소드 앞에 "@autowire @Qualifier ("myspecial ")를 넣어야합니다.

    EmployeeService 유형의 bean이 하나 뿐이며 EmployeeService 인터페이스에 다른 구현이없는 경우 Setter 메소드 앞에 EmployeeServiceImpl 및 @Autowire 앞에 "@Service"를 넣으면됩니다. 그렇지 않으면 @Service ( "myspecial")와 같은 특수 빈의 이름을 지정하고 setter 메소드 앞에 "@autowire @Qualifier ("myspecial ")를 넣어야합니다.

  5. ==============================

    5.컨트롤러 클래스에서 @ComponentScan ( "package") 주석을 추가하기 만하면됩니다. 필자의 경우 패키지 이름은 com.shoppingcart.So로 코드를 @ComponentScan ( "com.shoppingcart")로 작성하고 나에게 도움이되었습니다.

    컨트롤러 클래스에서 @ComponentScan ( "package") 주석을 추가하기 만하면됩니다. 필자의 경우 패키지 이름은 com.shoppingcart.So로 코드를 @ComponentScan ( "com.shoppingcart")로 작성하고 나에게 도움이되었습니다.

  6. ==============================

    6.서비스 클래스에서 @Service 주석을 잊어 버렸습니다.

    서비스 클래스에서 @Service 주석을 잊어 버렸습니다.

  7. ==============================

    7.@Service : 특정 클래스가 클라이언트에 대한 서비스임을 나타냅니다. 서비스 클래스는 주로 비즈니스 로직을 포함합니다. 패키지에 @Qualifier를 제공하는 것보다 많은 서비스 클래스가있는 경우 @Qualifier가 필요하지 않습니다.

    @Service : 특정 클래스가 클라이언트에 대한 서비스임을 나타냅니다. 서비스 클래스는 주로 비즈니스 로직을 포함합니다. 패키지에 @Qualifier를 제공하는 것보다 많은 서비스 클래스가있는 경우 @Qualifier가 필요하지 않습니다.

    사례 1 :

    @Service("employeeService")
    public class EmployeeServiceImpl implements EmployeeService{
    }
    

    사례 2 :

    @Service
    public class EmployeeServiceImpl implements EmployeeService{
    }
    

    두 가지 경우 모두 작동합니다 ...

  8. ==============================

    8.서비스 구현 클래스의 한정자 이름을 사용하여 아래 주석을 추가하기 만하면됩니다.

    서비스 구현 클래스의 한정자 이름을 사용하여 아래 주석을 추가하기 만하면됩니다.

    @Service("employeeService")
    @Transactional
    public class EmployeeServiceImpl implements EmployeeService{
    }
    
  9. ==============================

    9.impl 클래스에 'implements'키워드가 누락 되어도 문제가 될 수 있습니다.

    impl 클래스에 'implements'키워드가 누락 되어도 문제가 될 수 있습니다.

  10. from https://stackoverflow.com/questions/28547665/autowired-no-qualifying-bean-of-type-found-for-dependency-at-least-1-bean by cc-by-sa and MIT license