Announcement Announcement Module
Collapse
No announcement yet.
How to select different beans based on a property value? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to select different beans based on a property value?

    I would like to have the ability to select different beans based on a property value.

    For example if a property value is "A", I would like bean "someBean" to be aliased to to "someBean1" and if the property value is "B", I would like bean "someBean" to be aliased to "someBean2".

    Something like this might work:

    <if value="A" equals="${somePropertyValue}">
    <alias name="someBean1" alias="someBean"/>
    </if>

    <if value="B" equals="${somePropertyValue}">
    <alias name="someBean2" alias="someBean"/>
    </if>

    Does that make any sense?

    I didn't find anything in spring that supports conditional logic but perhaps I just missed it.

  • #2
    There isn't anything in the Spring xml configuration that can do that for you, however if you would use JavaConfig you could do this.

    You could also do this with xml by providing a FactoryBean which does what you suggest, return the correct instance based on a property.

    Comment


    • #3
      Maybe you should go one step back. Why do you want to switch the alias? To inject different beans depending on the property value? Why not use a placeholder for the ref of the bean property for which the beans get injected?

      Joerg

      Comment

      Working...
      X