@Autowired - 没有找到依赖至少 1 个 bean 类型的合格 bean
@Autowired - No qualifying bean of type found for dependency at least 1 bean
目前我在控制器和服务层之间的自动装配配置中遇到问题。
我无法追查我的错误。
简单日志信息
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: {}
下面我还给出了控制器和服务层代码以及调度程序-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;
}
}
调度员-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
您必须添加如下限定符名称:
@Service("employeeService")
应该可以解决您的问题
或在 @Service
之后,您应该添加 @Qualifier
注释,如下所示:
@Service
@Qualifier("employeeService")
伙计们,我发现了问题
我刚刚尝试在员工服务中添加限定符名称终于解决了我的问题。
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
}
您不一定非要提供名称和限定符。如果您设置了名称,那么该名称就是在上下文中注册 bean 所使用的名称。如果您不为您的服务提供名称,它将根据 BeanNameGenerator
注册为未大写的非限定 class 名称。因此,在您的情况下,实现将注册为 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 的情况。
如果你只有一个EmployeeService类型的bean,而EmployeeService接口没有其他实现,你可以简单的在EmployeeServiceImpl前加上“@Service”,在setter方法前加上“@Autowire”。
否则,您应该将特殊 bean 命名为 @Service("myspecial") 并在 setter 方法之前放置“@autowire @Qualifier("myspecial")。
在您的控制器 class 中,只需添加 @ComponentScan("package") 注释。在我的例子中,包名称是 com.shoppingcart.So 我将代码写为 @ComponentScan("com.shoppingcart") 并且它对我有用。
您在服务中忘记了@Service 注解class。
@Service:它告诉特定的 class 是对客户端的服务。服务 class 主要包含业务逻辑。如果包中的服务 class 比提供 @Qualifier 多,则不需要 @Qualifier。
案例 1:
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
}
案例2:
@Service
public class EmployeeServiceImpl implements EmployeeService{
}
两种情况都有效...
只需在服务实现中添加带有服务限定符名称的注释 class:
@Service("employeeService")
@Transactional
public class EmployeeServiceImpl implements EmployeeService{
}
impl 类 中缺少 'implements' 关键字也可能是问题所在
目前我在控制器和服务层之间的自动装配配置中遇到问题。
我无法追查我的错误。
简单日志信息
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: {}
下面我还给出了控制器和服务层代码以及调度程序-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;
}
}
调度员-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
您必须添加如下限定符名称:
@Service("employeeService")
应该可以解决您的问题
或在 @Service
之后,您应该添加 @Qualifier
注释,如下所示:
@Service
@Qualifier("employeeService")
伙计们,我发现了问题
我刚刚尝试在员工服务中添加限定符名称终于解决了我的问题。
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
}
您不一定非要提供名称和限定符。如果您设置了名称,那么该名称就是在上下文中注册 bean 所使用的名称。如果您不为您的服务提供名称,它将根据 BeanNameGenerator
注册为未大写的非限定 class 名称。因此,在您的情况下,实现将注册为 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 的情况。
如果你只有一个EmployeeService类型的bean,而EmployeeService接口没有其他实现,你可以简单的在EmployeeServiceImpl前加上“@Service”,在setter方法前加上“@Autowire”。 否则,您应该将特殊 bean 命名为 @Service("myspecial") 并在 setter 方法之前放置“@autowire @Qualifier("myspecial")。
在您的控制器 class 中,只需添加 @ComponentScan("package") 注释。在我的例子中,包名称是 com.shoppingcart.So 我将代码写为 @ComponentScan("com.shoppingcart") 并且它对我有用。
您在服务中忘记了@Service 注解class。
@Service:它告诉特定的 class 是对客户端的服务。服务 class 主要包含业务逻辑。如果包中的服务 class 比提供 @Qualifier 多,则不需要 @Qualifier。
案例 1:
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
}
案例2:
@Service
public class EmployeeServiceImpl implements EmployeeService{
}
两种情况都有效...
只需在服务实现中添加带有服务限定符名称的注释 class:
@Service("employeeService")
@Transactional
public class EmployeeServiceImpl implements EmployeeService{
}
impl 类 中缺少 'implements' 关键字也可能是问题所在