2014-03-31 52 views
0

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

associationApplicationContext.xml

例如

<!-- 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@Required标签在其setter方法。现在

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

,因为当我尝试在我的战争文件中使用这个bean -

我已经把JAR依赖于战争文件的pom.xml 已经把contextConfigLocationweb.xml

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

从WAR文件里的另一个applicationcontext.xml文件中使用bean作为

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

这里的物业associationClient是没有得到初始化,因为它无法找到参考豆associationRestClient这是associationServiceRestClient

我越来越

org.springframework.beans.factory.NoSuchBeanDefinitionException:无豆命名'associationServiceRestClient'被定义为

如何获得associationClient的对象在这里初始化?

PS:我不能改变它采用@Required注释

+1

在导入资源中,是不是类路径:/associationApplicationContext.xml.xml(.xml.xml)是一个错字? – Prasad

+0

修改了它。但这仅仅是一个例子。我不能把原始文件。 – Vishal

+0

您可以从web.xml中将applicationcontext.xml设置为servlet的配置参数吗? – Prasad

回答

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> 

您不必导入associationApplicationContext.xml在applicationcontext.xml作为现在的根应用程序bean将在applicationcontext.xml中可见。因此,应用程序Context.xml将如下所示:

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

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

+0

我解决了这个问题。 其实我需要将associationApplicationContext.xml导入servlet的contextConfigLocation而不是root。 这就是它能够识别和加载属性标记为@Required 感谢您的帮助反正。 :) – Vishal

+0

你知道它为什么工作:) – Prasad