3

我在验证器类中自动装配的两个服务有问题。这些服务工作正常,因为在我的控制器中是自动接线的。我有一个 applicationContext.xml 文件和 MyApp-servlet.xml 文件。我的基础包是 es.unican.meteo,而我的包 es.unican.meteo.validator 有问题。包 es.unican.meteo.controller 和 es.unican.meteo.service 可以正确地自动连接服务。

应用程序上下文.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 元素中看到控制器、服务和自动装配的元素。似乎 spring 没有检测到包验证器中的自动装配属性。有任何想法吗?

编辑:ResetPasswordValidation 的日志(Autowire 字段)

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
4

1 回答 1

3

确保对类进行注释,以便 Spring 将其作为 bean 拾取。自动装配只能发生在 DI 容器管理的 bean/类上。

添加@Component会导致类被Spring的组件扫描拾取,导致ResetPasswordValidation成为bean。此时,它应该有资格自动装配字段。

@Component
public class ResetPasswordValidation  implements Validator
于 2013-07-11T09:29:33.423 回答