invinc4u
invinc4u

Reputation: 1185

Making a spring bean dynamically refer to two classes based on a system property in Spring 3.0

I am trying to achieve the below functionality using Spring but have not succeeded till now.

Create a spring bean "testBean" in XML file and dynamically point it to class "A" or "B" depending on whether some system property "C" has been set or not. I want to handle this at the XML configuration file level itself. The rest of the application should be able to use the bean "testBean" seamlessly.

Please let me know how can this be done using Spring? Let me know if any other details are required.

Thanks in advance.

Upvotes: 3

Views: 1549

Answers (2)

stacker
stacker

Reputation: 69002

You could use the expression language to configure your testBean like this (not tested):

<bean id="testBean" class="com.test.TestBean">
    <property name="pointer" value="#{ systemProperties['C'] != null ? 'com.test.A' : 'com.test.B' }"/>
</bean>

See the documentation for further reference.

Relevant parts:

  • 6.4.1 XML based configuration
  • 6.5.13 Ternary Operator (If-Then-Else)

Upvotes: 3

Related Questions