2014-03-31 56 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