@Autowire批注的问题(空) [英] Problems with @Autowire annotation (null)

查看:125
本文介绍了@Autowire批注的问题(空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在验证程序类中自动接线的两个服务有问题.服务正常,因为在我的控制器中是自动接线的.我有一个applicationContext.xml文件和MyApp-servlet.xml文件.我的基本软件包是es.unican.meteo,而软件包es.unican.meteo.validator却有问题.包es.unican.meteo.controller和es.unican.meteo.service可以正确地自动连接服务.

I have problems with two services that i autowired in a validator class. The services works ok because in my controller are autowired. I've an applicationContext.xml file and MyApp-servlet.xml file. My base package is es.unican.meteo and i have problems with the package es.unican.meteo.validator. The package es.unican.meteo.controller and es.unican.meteo.service can autowire the services properly.

applicationContext.xml

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

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类:

Class 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没有在包验证器中检测到autowired属性.有什么想法吗?

I can see the controllers, services and autowired elements in the Spring elements. It seems like spring is not detecting the autowired properties in the package validator. Any ideas?

ResetPasswordValidation的日志(自动装配"字段)

Log of the ResetPasswordValidation (Autowire fields)

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

推荐答案

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

Make sure you annotate the class so Spring picks it up as a bean. Autowiring can only occur on beans/classes managed by the DI container.

添加@Component将导致该类被Spring的组件扫描拾取,从而导致ResetPasswordValidation成为bean.此时,应该可以自动连接字段了.

Adding @Component will cause the class to be picked up by Spring's component scanning, causing ResetPasswordValidation to become a bean. At this point, it should be eligible to have fields autowired.

@Component
public class ResetPasswordValidation  implements Validator

这篇关于@Autowire批注的问题(空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆