Announcement Announcement Module
Collapse
No announcement yet.
Choosing different bean implementation based on @Profile Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Choosing different bean implementation based on @Profile

    Is there a simple way to choose between two or more implementations (for example, one for development and one for production) without create two profiles for each selection?

    Let's say I have a FooFactory and I want to use a ServerBasedFooFactory in production but a StandaloneFooFactory in development.

    The simple solution would be to use two profiles (say "serverBased" and "notServerBased")

    Code:
    @Profile("serverBased")
    @Component
    class ServerBasedFooFactory implements FooFactory { }
    
    @Profile("notServerBased")
    @Component
    class StandaloneFooFactory implements FooFactory { }
    But I would like to have a the server based bean the default, and override it with the standalone one only if explicitly requested (so I don't need to create two profiles for FooFactory, then two additional for BarFactory, etc. in case I want more granularity than just "production" and "development" profiles).

    I could do it in XML configuration by importing an XML with overriding definition just when in development mode.
    Is there an easy way to accomplish this with annotations ?
Working...
X