在命名空间内使用Spring配置文件

时间:2012-11-12 13:28:04

标签: spring cxf jax-rs

您无需知道CXF就可以回答这个问题。

假设我有以下XML声明,它使用CXF创建JAXRS服务器

  <jaxrs:server id="customerService" address="/service1">
    <jaxrs:serviceBeans>
      <ref bean="serviceBean1" />
      <ref bean="serviceBean2" />
      <ref bean="serviceBean3" />
    </jaxrs:serviceBeans>
  </jaxrs:server>

现在,我需要找到一种方法,使用Spring配置文件机制为每个环境自定义此JAXRS服务器。我可以做类似的事情:

 <beans profile="dev">
    <jaxrs:server id="jaxrsServer" address="/service1">
      <jaxrs:serviceBeans>
        <ref bean="serviceBean2" />
        <ref bean="serviceBean3" />
      </jaxrs:serviceBeans>
    </jaxrs:server>
  </beans>

  <beans profile="prod">
    <jaxrs:server id="jaxrsServer" address="/service1">
      <jaxrs:serviceBeans>
        <ref bean="serviceBean1" />
        <ref bean="serviceBean2" />
      </jaxrs:serviceBeans>
    </jaxrs:server>
  </beans>

但这不是我想要的,因为我的真实世界JAXRS服务器比我的例子更复杂。

我正在寻找的是:

<jaxrs:server id="jaxrsServer" address="/service1">
  <jaxrs:serviceBeans>
    <beans profile="dev">
      <ref bean="serviceBean1" />
      <ref bean="serviceBean2" />
    </beans>
    <beans profile="dev">
      <ref bean="serviceBean2" />
      <ref bean="serviceBean3" />
    </beans>
  </jaxrs:serviceBeans>
</jaxrs:server>

但它不起作用。


我试图外化列表创建:

  <bean id="serviceBeansList" class="java.util.ArrayList">
    <constructor-arg>
      <list>
        <ref bean="serviceBean1"/>
        <ref bean="serviceBean2"/>
      </list>
    </constructor-arg>
  </bean>

<jaxrs:server id="jaxrsServer" address="/service1">
  <jaxrs:serviceBeans>
      <ref bean="serviceBeansList" />
  </jaxrs:serviceBeans>
</jaxrs:server>

它仍然不起作用,因为CXF命名空间处理程序没有“展平”列表,因此它不处理2个serviceBeans对象的列表,而是处理1个ArrayList的列表,它不是由CXF处理的类型

Spring XML解析尝试获取列表。我发现在CXF命名空间处理代码中:

if ( "serviceBeans".equals(name) ) {
            List<?> list = ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
            bean.addPropertyValue(name, list);
}

不是引用bean,是不是可以用Spring引用“xml声明片段”或其他可以工作的东西?

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用别名+个人资料

<jaxrs:server id="jaxrsServer" address="/service1">
  <jaxrs:serviceBeans>
    <ref bean="serviceBeanA" />
    <ref bean="serviceBeanB" />
  </jaxrs:serviceBeans>
</jaxrs:server>

<beans profile="profile1">
  <alias name="serviceBean1" alias="serviceBeanA"/>
  <alias name="serviceBean2" alias="serviceBeanB" />
</beans>
<beans profile="profile2">
  <alias name="serviceBean3" alias="serviceBeanA"/>
  <alias name="serviceBean4" alias="serviceBeanB" />
</beans>
免责声明:这个答案纯属幻想,但评论有些限制 - 如果不起作用,我会删除