开发者

Conditions in Spring expression language(SpEL) used in bean definition

As far SpEL is used in Spring 3.0,

I would like to ask, is it possible to do following(in bean definition .xml):

<c:choose>

  <c:when test="#{prop=='a'}">
   <bean class="BeanA"/>
  </c:when&g开发者_开发问答t;

  <c:otherwise>
   <bean class="BeanB"/>
  </c:otherwise>

</c:choose>

Someth. like in jstl.

Thank you for help.


Environment profiles/Environment specific beans will be available in Spring 3.1 which should be released shortly - so you might want to wait for that.

There is no built in support for conditional beans in Spring 3.0. However, it could be achieved by using PropertyPlaceholderConfigurers and/or FactoryBeans.


There's no conditional mechanism for XML Spring bean defintion files. However, maybe this would work:

<bean class="#{prop=='a' ? BeanA : BeanB}"/>

But even if this approach worked, it wouldn't be the most readable one. My suggestion would be to use different set of XML configuration files and pick them depending on some global settings. Naturally you would put all the common beans (i.e. these whose definition is always the same) in a separate file and have it always included.


it is not a question of using spel, but more of XML,afaik you can't do this in XML (but xslt)

the proper spring way for this scenario might be http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-class in combination with a "parent" interface for BeanA and BeanB

you could pass the parameter (system ? runtime specific ?) to the factory, which would create either BeanA or BeanB

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜