【问题标题】:@Autowired - No qualifying bean of type found for dependency at least 1 bean@Autowired - 没有为依赖至少 1 个 bean 找到符合条件的 bean
【发布时间】:2015-04-17 07:58:19
【问题描述】:

目前我在控制器和服务层之间的 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();


}

服务接口实现

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>

【问题讨论】:

  • 可以加@Service("employeeService")

标签: spring spring-mvc spring-annotations


【解决方案1】:

我相信对于@Service,您必须添加如下限定符名称:

@Service("employeeService") 应该可以解决您的问题

或在@Service 之后添加@Qualifier 注释,如下所示:

@Service
@Qualifier("employeeService")

【讨论】:

【解决方案2】:

我发现了问题

我刚刚尝试在员工服务中添加限定符名称,终于解决了我的问题。

@Service("employeeService")

public class EmployeeServiceImpl implements EmployeeService{

}

【讨论】:

    【解决方案3】:

    您不必提供名称和限定符。如果你设置了一个名字,那就是 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。

    【讨论】:

    • 如果我有@Repository 而不是@Service 是一样的吗?
    • @SamuelDavidGómezRamos - 无论什么春天的刻板印象都是一样的。你可以很容易地验证它。
    【解决方案4】:

    如果你只有一个EmployeeService类型的bean,而EmployeeService接口没有其他实现,你可以简单地把“@Service”放在EmployeeServiceImpl前面,把“@Autowire”放在setter方法前面。 否则,您应该将特殊 bean 命名为 @Service("myspecial") 并将 "@autowire @Qualifier("myspecial") 放在 setter 方法之前。

    【讨论】:

      【解决方案5】:

      在您的控制器类中,只需添加 @ComponentScan("package") 注释。在我的情况下,包名称是 com.shoppingcart。所以我将代码编写为 @ComponentScan("com.shoppingcart") 并且它对我有用。

      【讨论】:

        【解决方案6】:

        您在服务类中忘记了@Service 注释。

        【讨论】:

          【解决方案7】:

          @Service:它告诉特定类是客户端的服务。服务类主要包含业务逻辑。如果你的包中的服务类比提供@Qualifier 的多,否则它不应该需要@Qualifier。

          案例一:

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

          案例2:

          @Service
          public class EmployeeServiceImpl implements EmployeeService{
          }
          

          两种情况都有效...

          【讨论】:

            【解决方案8】:

            只需在服务实现类中添加以下带有限定符名称的注释即可:

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

            【讨论】:

              【解决方案9】:

              在 impl 类中缺少“implements”关键字也可能是问题

              【讨论】:

                猜你喜欢
                • 2013-12-18
                • 2017-10-01
                • 1970-01-01
                • 2017-01-16
                • 1970-01-01
                • 2020-04-02
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多