2015-02-16 15 views
7

Hiện tại tôi đang gặp sự cố trong cấu hình Autowire giữa bộ điều khiển và lớp dịch vụ.@Autowired - Không có loại đậu đủ điều kiện được tìm thấy cho phụ thuộc ít nhất 1 bean

Tôi không thể theo dõi lỗi của mình.

Simple Log Info

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: {} 

Dưới đây tôi cũng đã đưa ra các điều khiển và dịch vụ lớp mã và cũng dispatcher-servlet.xml

khiển

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; 
     } 

    } 

Giao diện dịch vụ

package com.ott.service; 

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

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

    List<Employee> fetchAll(); 


} 

Interface Dịch vụ 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> 
+0

bạn có thể thêm @Service ("e mployeeService ") –

Trả lời

4

Bạn không nhất thiết phải cung cấp tên và vòng loại. Nếu bạn đặt tên, đó là tên mà bean được đăng ký trong ngữ cảnh. Nếu bạn không cung cấp tên cho dịch vụ của mình, tên này sẽ được đăng ký là tên lớp không đủ điều kiện không hợp lệ dựa trên BeanNameGenerator. Vì vậy, trong trường hợp của bạn, việc thực hiện sẽ được đăng ký là employeeServiceImpl. Vì vậy, nếu bạn cố gắng autowire với tên đó, nó nên giải quyết trực tiếp.

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 được sử dụng trong trường hợp nếu có nhiều loại đậu cùng loại và bạn muốn tự động thực hiện các bean khác nhau cho các mục đích khác nhau.

3

Tôi tin cho @Service bạn phải thêm tên vòng loại như dưới đây:

@Service("employeeService") nên giải quyết vấn đề của bạn

hoặc sau @Service bạn nên thêm @Qualifier annontion như dưới đây:

@Service 
@Qualifier("employeeService") 
+0

Cảm ơn Ramesh đã đọc tài liệu này vào mùa xuân http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#beans-scanning- autodetection và cuối cùng đã giải quyết được vấn đề – Arun

+0

Ramesh @Service không cần phải luôn có tên và vòng loại. Đọc câu trả lời của tôi cho rõ ràng. Tôi không muốn câu trả lời của bạn để hiểu lầm. – minion

+0

@minion, bạn là chính xác, theo OP nó là cần thiết –

5

Guys Tôi thấy vấn đề

Tôi chỉ cố gắng bằng cách thêm tên vòng loại phục vụ người lao động cuối cùng nó đã giải quyết được vấn đề của tôi.

@Service("employeeService") 

public class EmployeeServiceImpl implements EmployeeService{ 

} 
+0

Đó là những gì mà @Ramesh Kotha đã nói. –

0

Nếu bạn chỉ có một loại bean EmployeeService và giao diện EmployeeService không có triển khai khác, bạn có thể chỉ cần đặt "@Service" trước EmployeeServiceImpl và "@Autowire" trước phương thức setter. Nếu không, bạn nên đặt tên cho bean đặc biệt như @Service ("myspecial") và đặt "@autowire @Qualifier (" myspecial ") trước phương thức setter.

0

Trong lớp điều khiển của bạn, chỉ cần thêm @ComponentScan (" package Trong trường hợp của tôi tên gói là com.shoppingcart.Vì vậy, tôi đã viết mã như @ComponentScan ("com.shoppingcart") và nó đã làm việc cho tôi.

0

Bạn quên chú thích @Service trong lớp dịch vụ của mình.

0

@Service: Nó nói rằng lớp cụ thể là Dịch vụ cho khách hàng. Lớp dịch vụ chứa chủ yếu là logic nghiệp vụ. Nếu bạn có nhiều lớp dịch vụ trong một gói hơn cung cấp @Qualifier nếu không nó sẽ không yêu cầu @Qualifier.

Trường hợp 1:

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

Trường hợp2:

@Service 
public class EmployeeServiceImpl implements EmployeeService{ 
} 

cả hai trường hợp đang làm việc ...

0

Chỉ cần thêm bên dưới chú thích với tên vòng loại dịch vụ trong lớp thực hiện dịch vụ:

@Service("employeeService") 
@Transactional 
public class EmployeeServiceImpl implements EmployeeService{ 
} 
Các vấn đề liên quan