11

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

4

2 回答 2

23

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

并且不要忘记启用注释配置 bean 后处理器(请参阅 <context:annotation-config /> 元素)

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

如何启用您的 Validation 类作为托管 Spring bean。任何一个

通过使用xml(如上图)

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

通过使用注释代替(注意@Component就在类上方)

@Component
public class AccessRequestValidator implements Validator {

}

但是要启用 Spring 注释组件扫描,您必须启用 bean-post 处理器(注意 <context:component-scan 元素)

<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 上下文参数和 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>

您的 <SPRING-SERVLET-NAME>-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>
于 2010-04-29T03:12:33.533 回答
0

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

上下文.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>

扫描仪是否递归扫描树?

于 2010-04-29T16:21:39.363 回答