11

Validation クラスでオブジェクトを Autowire することは可能ですか? Autowired であるはずのオブジェクトに対して null を取得し続けます...

4

2 に答える 2

23

Validation クラスは有効な Spring Beanですか??? そうでない場合は、オートワイヤーされたオブジェクトに対して常に null を取得します。検証クラスが有効になっていることを確認してください。

Annotation config 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 processor を有効にする必要があります (<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>

コントローラー内で、それを行うだけです(new 演算子は使用しないでください)

次の戦略のいずれかを選択します

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

}

アップデート

Web アプリの構造は次のようになります。

<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は次のようになります (NOTICE 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>

<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

あなたが上に示したものに従おうとすると、私はまだnullポインタを取得しています:

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;
...
}

business-context.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 に答える