无法使用使用RequiredAnnotationBeanPostProcessor的公开bean

时间:2014-03-31 13:18:39

标签: java spring maven spring-mvc annotations

我正在创建一个战争,它将使用jar文件中的一个类。 现在这个jar文件在associationApplicationContext.xml中有一个公开的bean,它在其某些属性上使用@Required注释进行初始化。

associationApplicationContext.xml

e.g。

<!-- processes @Required annotations-->
  <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
  <!-- The client bean to access the association rest web service -->
  <bean id="associationClient" class="com.springtest.client.AssociationClientImpl">
      <property name="associationRestClient" ref="associationServiceRestClient" />
  </bean>

因此,在AssociationClient内,属性associationRestClient在其setter方法上有@Required标记。

@Required
public void setAssociationRestClient(final AssociationRestClient associationRestClient) {
        this.associationRestClient= associationRestClient;
 }

现在,就像我在war文件中尝试使用这个bean一样 -

我已经将jar依赖项放在war文件的pom.xml中了 已将contextConfigLocation放在web.xml

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:/associationApplicationContext.xml
    </param-value>
    </context-param>

将另一个applicationcontext.xml文件中war文件内的bean用作

 <import resource="classpath:/associationApplicationContext.xml"/>
    <bean id="requestHandlerV1"
            class="com.springtest.application.RequestHandlerV1">
            <property name="associationClient" ref="associationClient"></property>      
    </bean>

此处属性associationClient未初始化,因为它无法找到对associationRestClient

的bean associationServiceRestClient的引用

我正在

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为&#39; associationServiceRestClient&#39;已定义

如何在此处初始化associationClient的对象?

PS:我无法更改使用@Required注释的实现

1 个答案:

答案 0 :(得分:0)

假设您有以下内容: 在web.xml中:

    <!-root application context-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:associationApplicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>     
    <servlet>
        <servlet-name>JerseySpringWebApplication</servlet-name> 
        <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> 
        <init-param> 
            <param-name>contextConfigLocation</param-name> 
            <param-value>classpath*:applicationcontext.xml</param-value> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <!--URl for web service --> 
    <servlet-mapping> 
        <servlet-name>JerseySpringWebApplication</servlet-name> 
        <url-pattern>/service/*</url-pattern> 
    </servlet-mapping>

您不必在applicationcontext.xml中导入associationApplicationContext.xml,因为现在根应用程序bean将在applicationcontext.xml中可见。所以应用Context.xml就像:

    <mvc:annotation-config/>
    <bean id="requestHandlerV1" class="com.springtest.application.RequestHandlerV1">
        <property name="associationClient" ref="associationClient"></property>      
    </bean>

*假设您通过休息呼叫调用控制器 如果您仍然看到此问题,请发布您的日志。