Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

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

</c:choose>

Someth. like in jstl.

Thank you for help.

like image 300
sergionni Avatar asked Feb 25 '23 18:02

sergionni


2 Answers

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.

like image 52
Flemming Avatar answered Apr 30 '23 09:04

Flemming


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.

like image 24
Grzegorz Oledzki Avatar answered Apr 30 '23 08:04

Grzegorz Oledzki