@Autowired-没有找到至少1个依赖类型的合格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>

问题答案:

您不必一定提供名称和限定词。如果您设置一个名称,那就是在上下文中注册该bean的名称。如果您没有为服务提供名称,它将基于进行注册为无大写的非合格类名称BeanNameGenerator。因此,在您的情况下,实现将注册为employeeServiceImpl。因此,如果您尝试使用该名称自动接线,则应直接解析。

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,则使用。