目前我在控制器和服务层之间遇到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: {}
下面我还给出了Controller和Service Layer代码以及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;
}
}
分派器-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>
答案 0 :(得分:8)
伙计们我发现了这个问题
我刚尝试在员工服务中添加限定符名称,最终解决了我的问题。
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
}
答案 1 :(得分:5)
我相信@Service
您必须添加限定符名称,如下所示:
@Service("employeeService")
应解决您的问题
或@Service
后,您应添加@Qualifier
annontion,如下所示:
@Service
@Qualifier("employeeService")
答案 2 :(得分:5)
您不必提供名称和限定符。如果设置名称,那就是在上下文中注册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;
}
如果存在多个相同类型的bean并且您希望为各种目的自动装配不同的实现bean,则使用 @Qualifier
。
答案 3 :(得分:0)
如果您只有一个EmployeeService类型的bean,并且接口EmployeeService没有其他实现,您可以简单地将&#34; @Service&#34;在EmployeeServiceImpl和&#34; @ Autowire&#34;之前在setter方法之前。 否则,您应该将特殊bean命名为@Service(&#34; myspecial&#34;)并在setter方法之前放置&#34; @autowire @Qualifier(&#34; myspecial&#34;)。
答案 4 :(得分:0)
在您的控制器类中,只需添加@ComponentScan(&#34; package&#34;)注释。在我的情况下,包名称是com.shoppingcart.So我将代码编写为@ComponentScan(&#34; com.shoppingcart&#34;),它对我有用。
答案 5 :(得分:0)
您忘记了服务类中的@Service注释。
答案 6 :(得分:0)
@Service:它告诉特定的类是客户端的服务。服务类主要包含业务逻辑。如果包中有多个Service类而不是提供@Qualifier,否则它不应该要求@Qualifier。
案例1:
<
情况2:
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
}
两种情况都在起作用......
答案 7 :(得分:0)
只需在服务实现类中添加带有限定符服务名称的注释:
@Service("employeeService")
@Transactional
public class EmployeeServiceImpl implements EmployeeService{
}
答案 8 :(得分:-1)
缺少&#39;工具&#39; impl类中的关键字也可能是问题