gpt4 book ai didi

spring - @Autowire注释的问题(空)

转载 作者:行者123 更新时间:2023-12-02 08:53:44 31 4
gpt4 key购买 nike

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

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 元素中看到 Controller 、服务和 Autowiring 元素。似乎 spring 没有检测到包验证器中的 Autowiring 属性。有什么想法吗?

编辑: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

最佳答案

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

添加@Component将导致Spring的组件扫描拾取该类,从而导致ResetPasswordValidation成为一个bean。此时,它应该可以 Autowiring 字段。

@Component
public class ResetPasswordValidation implements Validator

关于spring - @Autowire注释的问题(空),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17589757/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com