Spring Expression Language (SpEL) Conditions Used in a Bean Definition

While SpEL is used in Spring 3.0,

I would like to ask if it is possible to do the following (in a bean definition of .xml):

<c:choose> <c:when test="#{prop=='a'}"> <bean class="BeanA"/> </c:when> <c:otherwise> <bean class="BeanB"/> </c:otherwise> </c:choose> 

Someth. as in jstl.

Thank you for your help.

+4
source share
3 answers

Environment Profiles / The environment-specific environment for beans will be available in Spring 3.1, which is due out soon, so you can wait.

There is no native support for conditional beans in Spring 3.0. However, this can be achieved using PropertyPlaceholderConfigurers and / or FactoryBeans.

+5
source

There are no conditional mechanisms for Spring bean XML defrag files. However, perhaps this will work:

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

But even if this approach worked, it would not be the most readable one. My suggestion was to use a different set of XML configuration files and select them depending on some global settings. Naturally, you would put all the ordinary beans (i.e. those whose definition is always the same) in a separate file and always include it.

+2
source

it is not a question of using spel, but more XML, afaik you cannot do it in XML (but xslt)

The correct spring way for this scenario could be http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-class combined with the "parent" interface for BeanA and BeanB

you can pass the parameter (time?) to the factory, which would create a BeanA or BeanB

0
source

All Articles