在基于java的配置中覆盖xml定义的spring bean

时间:2015-09-21 10:12:47

标签: java spring cxf hippocms

我使用自己的REST界面扩展了一个名为Hippo CMS的完整产品。 Hippo CMS正在使用Apache CXF进行休息,并从Hippo CMS源中某处定义的spring bean获取资源定义。这个定义如下:

<bean id="jaxrsRestPlainResourceProviders" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="targetClass" value="org.apache.commons.collections.ListUtils" />
  <property name="targetMethod" value="union" />
  <property name="arguments">
    <list>
      <ref bean="customRestPlainResourceProviders" />
      <ref bean="defaultRestPlainResourceProviders" />
    </list>
  </property>
</bean>

<bean id="defaultRestPlainResourceProviders" class="org.springframework.beans.factory.config.ListFactoryBean">
  <property name="sourceList">
    <list>
    </list>
  </property>
</bean>

<!-- Default empty list of custom plain resource providers to be overriden. -->
<bean id="customRestPlainResourceProviders" class="org.springframework.beans.factory.config.ListFactoryBean">
  <property name="sourceList">
    <list>
    </list>
  </property>
</bean>

我需要用自己的bean覆盖customRestPlainResourceProviders bean。它在XML配置中运行良好,如下所示:

<bean id="customRestPlainResourceProviders" class="org.springframework.beans.factory.config.ListFactoryBean">
  <property name="sourceList">
    <list>
      <bean class="org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider">
        <constructor-arg>
          <bean class="com.xxx.rest.FolderStructureResource"/>
        </constructor-arg>
      </bean>
    </list>
  </property>
</bean>

但是如果我在Java配置类中定义一个bean(在其他bean工作正常的情况下),它就不起作用了:

@Bean(name = "customRestPlainResourceProviders")
public ListFactoryBean customRestPlainResourceProviders() {
  ListFactoryBean listFactoryBean = new ListFactoryBean();
  listFactoryBean.setSourceList(
    Lists.newArrayList(
      new SingletonResourceProvider(
        new FolderStructureResource(repository())
      )
    )
  );
  return listFactoryBean;
}

是否有办法使用在Java配置类中创建的bean覆盖XML配置中定义的bean?

0 个答案:

没有答案