2013-07-11 4 views
3

나는 validator 클래스에서 autowired하는 두 가지 서비스에 문제가 있습니다. 내 컨트롤러가 autowired이기 때문에 서비스가 제대로 작동합니다. 나는 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")); 
     } 
    } 
} 

나는 컨트롤러, 서비스를 확인하고 스프링 요소의 요소를 autowire가 있습니다 . 그것은 봄이 패키지 유효성 검사기에서 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

는 스프링 빈으로 집어 있도록 클래스에 주석을해야합니다. 자동 와이어 링은 DI 컨테이너가 관리하는 bean/클래스에서만 발생합니다.

@Component을 추가하면 클래스 구성 요소 스캔에 의해 클래스가 선택되어 ResetPasswordValidation이 빈이됩니다. 이 시점에서, autowired 필드를 가질 자격이 있어야합니다.

+0

@mannuk 행운? –

+0

당신의 요점은 매우 흥미 롭습니다 @ 케빈. 이제 eclipse 프로젝트는 유효성 검사 클래스에 autowired 요소가 있음을 감지하지만 서비스를 주입하지 않습니다. 속성은 여전히 ​​null입니다. 아마도 더 많은 것들이 필요합니까? – mannuk

+0

@mannuk 서비스에 @Service 주석을 달았습니까? –