【问题标题】:Unable to use exposed bean which uses RequiredAnnotationBeanPostProcessor无法使用使用 RequiredAnnotationBeanPostProcessor 的暴露 bean
【发布时间】:2014-03-31 13:18:38
【问题描述】:

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

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 在其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 没有被初始化,因为它找不到对bean associationRestClient 的引用,即associationServiceRestClient

我来了

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“associationServiceRestClient”的bean

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

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

【问题讨论】:

  • 在导入资源中,classpath:/associationApplicationContext.xml.xml (.xml.xml) 不是错字吗?
  • 已更正。但这只是一个例子。我不能放原始文件。
  • 你能从 web.xml 中发布 sn-p,其中 applicationcontext.xml 被设置为 servlet 的配置参数吗?
  • JerseySpringWebApplicationcom.sun.jersey.spi.spring.container.servlet.SpringServletcontextConfigLocationclasspath:/applicationcontext.xml1-on-startup> JerseySpringWebApplication/service/*模式>
  • 这里。我贴出来了。对不起(非)格式。

标签: java spring maven spring-mvc annotations


【解决方案1】:

假设您有以下内容: 在 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>

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

【讨论】:

  • 我解决了这个问题。实际上我需要将 associationApplicationContext.xml 导入 servlet 的 contextConfigLocation 而不是 root。那是它能够识别并加载具有标记为@Required 的属性的bean 无论如何感谢您的帮助。 :)
猜你喜欢
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多