2013-07-11 62 views
3

我在驗證器類中自動裝配了兩個服務時遇到問題。這些服務工作正常,因爲在我的控制器中是自動裝配的。我有一個applicationContext.xml文件和MyApp-servlet.xml文件。我的基礎包是es.unican.meteo,並且我在es.unican.meteo.validator包中遇到問題。 es.unican.meteo.controller和es.unican.meteo.service包可以自動正確地裝載服務。@Autowire註釋問題(null)

的applicationContext.xml

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

.... 
some beans 
... 
</beans> 

MYAPP-servlet.xml中

<?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: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-3.1.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

    <!-- Enabling Spring beans auto-discovery --> 
    <context:component-scan base-package="es.unican.meteo" /> 

    <!-- Enabling Spring MVC configuration through annotations --> 
    <mvc:annotation-driven /> 

類ResetPasswordValidator:

package es.unican.meteo.validator; 


import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.validation.Errors; 
import org.springframework.validation.Validator; 

import es.unican.meteo.model.User; 
import es.unican.meteo.service.MessageService; 
import es.unican.meteo.service.UserService; 

public class ResetPasswordValidation implements Validator{ 

    @Autowired 
    private UserService userService; 

    @Autowired 
    private MessageService messageService; 

    public boolean supports(Class<?> clazz) { 
     return User.class.equals(clazz); 
    } 

    public void validate(Object target, Errors errors) { 
     User user = (User)target; 
     if(userService.getUserByEmail(user.getEmail())==null){ 
      errors.rejectValue("email", messageService.getMessage("app.error.nonexistentemail")); 
     } 
    } 
} 

我可以看到控制器,服務,並在彈簧元件自動裝配元素。看起來Spring似乎沒有檢測到包驗證器中的autowired屬性。有任何想法嗎?

編輯:ResetPasswordValidation(自動裝配領域)的登錄

12:48:50,697 DEBUG main support.DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'resetPasswordValidation' 
12:48:50,697 DEBUG main support.DefaultListableBeanFactory:430 - Creating instance of bean 'resetPasswordValidation' 
12:48:50,701 DEBUG main annotation.InjectionMetadata:60 - Found injected element on class [es.unican.meteo.validator.ResetPasswordValidation]: AutowiredFieldElement for private es.unican.meteo.service.UserService es.unican.meteo.validator.ResetPasswordValidation.userService 
12:48:50,702 DEBUG main annotation.InjectionMetadata:60 - Found injected element on class [es.unican.meteo.validator.ResetPasswordValidation]: AutowiredFieldElement for private es.unican.meteo.service.MessageService es.unican.meteo.validator.ResetPasswordValidation.messageService 
12:48:50,702 DEBUG main support.DefaultListableBeanFactory:504 - Eagerly caching bean 'resetPasswordValidation' to allow for resolving potential circular references 
12:48:50,707 DEBUG main annotation.InjectionMetadata:85 - Processing injected method of bean 'resetPasswordValidation': AutowiredFieldElement for private es.unican.meteo.service.UserService es.unican.meteo.validator.ResetPasswordValidation.userService 

回答

3

確保您註釋類,所以春天撿起來,作爲一個bean。自動裝配只能發生在由DI容器管理的beans/classes上。

添加@Component將導致該類被Spring的組件掃描拾取,導致ResetPasswordValidation成爲一個bean。此時,它應該有資格擁有自動裝配的字段。

+0

@mannuk對此有幫助嗎?運氣好的話? –

+0

你的觀點非常有趣@Kevin。現在eclipse項目檢測到我在驗證類中有自動裝配的元素,但它沒有注入這些服務。屬性仍然爲空,也許我需要更多的東西? – mannuk

+0

@mannuk您是否使用@Service註釋了您的服務? –