【问题标题】:How to @Autowire objects in Validator classes?如何@Autowire 验证器类中的对象?
【发布时间】:2010-04-28 20:41:18
【问题描述】:

是否可以在 Validation 类中自动装配对象?对于应该是 Autowired 的对象,我一直为 null...

【问题讨论】:

  • 你能举个例子吗?
  • 这与我的项目布局相同。更令人困惑的是@Autowire 正在处理@Controller 类。问题是我在控制器内部实例化 AccessRequestValidator 类(而不是在上下文 xml 文件中设置它)。我已经用@Component 注释了Validator,但没有一个@Autowire 工作......

标签: spring autowired


【解决方案1】:

您的验证类是启用的 Spring bean 吗?如果没有,您的对象自动装配总是会为 null。确保您已启用验证类。

并且不要忘记启用注释配置 bean 后处理器(请参阅 元素)

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:annotation-config />
</beans>

如何启用您的验证类作为托管 Spring bean。要么

通过使用xml(如上图)

<beans ...>
    <bean class="AccessRequestValidator"/>
    <context:annotation-config />
</beans>

改用注解(注意 @Component 就在类的上方)

@Component
public class AccessRequestValidator implements Validator {

}

但是要启用 Spring 注释组件扫描,您必须启用 bean-post 处理器(注意

<beans ...>
    <context:annotation-config />
    <context:component-scan base-package="<PUT_RIGHT_HERE_WHICH_ROOT_PACKAGE_SHOULD_SPRING_LOOK_FOR_ANY_ANNOTATED_BEAN>"/>
</beans>

在您的控制器中,只需执行此操作(不要使用新运算符)

选择以下策略之一

public class MyController implements Controller {

    /**
      * You can use FIELD @Autowired
      */
    @Autowired
    private AccessRequestValidator accessRequestValidator;

    /**
      * You can use PROPERTY @Autowired
      */
    private AccessRequestValidator accessRequestValidator;
    private @Autowired void setAccessRequestValidator(AccessRequestValidator accessRequestValidator) {
        this.accessRequestValidator = accessRequestValidator;
    }

    /**
      * You can use CONSTRUCTOR @Autowired
      */
    private AccessRequestValidator accessRequestValidator;

    @Autowired
    public MyController(AccessRequestValidator accessRequestValidator) {
        this.accessRequestValidator = accessRequestValidator;
    }   

}

更新

您的网络应用程序结构应如下所示

<CONTEXT-NAME>/
       WEB-INF/
           web.xml
           <SPRING-SERVLET-NAME>-servlet.xml
           business-context.xml
           classes/
               /com
                   /wuntee
                       /taac
                           /validator
                               AccessRequestValidator.class
           lib/
               /**
                 * libraries needed by your project goes here
                 */

您的 web.xml 应该看起来像(注意 contextConfigLocation context-param 和 ContextLoaderListener)

<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!--If your business-context.xml lives in the root of classpath-->
        <!--replace by classpath:business-context.xml-->
        <param-value>
            /WEB-INF/business-context.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name><SPRING-SERVLET-NAME></servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name><SPRING-SERVLET-NAME></servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

您的 -servlet.xml 应该看起来像(注意我使用的是 Spring 2.5 - 如果您使用的是 3.0,请替换)

 <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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!--ANY HANDLER MAPPING-->
    <!--ANY VIEW RESOLVER-->
    <context:component-scan base-package="com.wuntee.taac"/>
    <context:annotation-config/>
</beans>

【讨论】:

  • 我认为他指的是由 Spring 管理的验证器类,在这种情况下 annotation-config 不会有什么不同。
  • 我不确定你所说的“由 spring 管理/非管理”是什么意思...我有一个实现 Validator 并在控制器中使用的类,例如:AccessRequestValidator validator = new AccessRequestValidator() ; validator.validate(accessRequestBean, result);
  • @wuntee 您确实没有 Spring 托管 bean。如果您使用 new 运算符创建一些实例,Spring 如何管理您的实例?我会告诉你如何实现你的目标
【解决方案2】:

尝试按照您上面显示的内容,我仍然得到一个空指针:

context.xml:

<context:annotation-config />
<context:component-scan base-package="com.wuntee.taac"/>

AccessRequestValidator.java

package com.wuntee.taac.validator;

@Component
public class AccessRequestValidator implements Validator {

    @Autowired
    private UserAccessCache userAccessCache;
...
}

业务上下文.xml:

   <bean id="userAccessCache" class="com.wuntee.taac.controller.UserAccessCache">
        <property name="cadaDao" ref="cadaDao" />
        <property name="adDao" ref="adDao" />
   </bean>

扫描器是否递归地扫描树?

【讨论】:

  • 奇怪,我现在在我的项目中看到了同样的东西。带有@Autowired 字段的@Component-annotated Validator,但它从未连接并保持为空。
  • 更新:我发现了我的错误。在我的控制器中,我有一个带有以下代码的 @InitBinder 方法: binder.setValidator(new FooValidator());因此,当 Spring 管理 a FooValidator 时,我将控制器设置为使用不同的非托管控制器。我只是将其更改为自动装配的 FooValidator。
猜你喜欢
  • 1970-01-01
  • 2021-05-17
  • 2022-01-10
  • 2018-08-21
  • 2021-03-09
  • 2017-03-16
  • 2021-04-22
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多