Spring @Autowire注释存在问题(null)
我在验证器类中自动连接的两个服务有问题。服务工作正常,因为在我的控制器中是自动连接的。我有一个applicationContext.xml文件和MyApp-servlet.xml文件。我的基本包是es.unican.meteo,我的包es.unican.meteo.validator有问题。包es.unican.meteo.controller和es.unican.meteo.service可以正确地自动连接服务 applicationContext.xmlSpring @Autowire注释存在问题(null),spring,service,spring-3,autowired,Spring,Service,Spring 3,Autowired,我在验证器类中自动连接的两个服务有问题。服务工作正常,因为在我的控制器中是自动连接的。我有一个applicationContext.xml文件和MyApp-servlet.xml文件。我的基本包是es.unican.meteo,我的包es.unican.meteo.validator有问题。包es.unican.meteo.controller和es.unican.meteo.service可以正确地自动连接服务 applicationContext.xml <?xml version="
<?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>
确保您对该类进行了注释,以便Spring将其作为bean进行处理。自动连接只能发生在DI容器管理的bean/类上 添加
@Component
将导致Spring的组件扫描拾取该类,从而导致ResetPasswordValidation
成为bean。此时,应该可以自动连接字段
@Component
public class ResetPasswordValidation implements Validator
凯文,你的观点很有趣。现在eclipse项目检测到我在验证类中有自动连接的元素,但它没有注入服务。属性仍然为空,也许我需要更多的东西吗?是的@Kevin,控制器类中的服务工作正常。我已经添加了与ResetPasswordValidationI发现错误@kevin对应的日志。我正在用new(…)创建ResetPasswordValidation的实例;现在,我的控制器中有一个自动连接的验证器属性。非常感谢您的回答,因为组件是必需的@曼努克很高兴我能帮忙。我以前在new上也犯过错误。我会看看你的博客:)
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"));
}
}
}
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
@Component
public class ResetPasswordValidation implements Validator