Announcement Announcement Module
No announcement yet.
Environment based bean configuration/loading Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Environment based bean configuration/loading

    I have a situation where there is a set of beans tied to an environment. i.e. When my application runs in a test environment i have to load a specific set of beans and when it runs in production it has to load a specific set of beans.

    Essentially, i can have two xml configuration files with environment specific beans or can have a single file with all the beans. But the problem is how do I load/switch beans based on the environment ?

    I am using standalone Spring container to load all my beans. One option that I thought of was to have two files in separate folders (test, prod) and then specify enviornment specific folder when I initialize my container.

    private final static ApplicationContext springContext = 
    SpringApplicationContextSupport.prepareSpringApplicationContext("classpath*:spring/<test or prod>/config.xml"  );
    Is there a tool or a better way (annotations ?) to achieve this ?

    Appreciate your help

  • #2
    I did a search which brought up some threads where this has been discussed:

    One of the solution was to use variables to supply values from property files - which is what I am also doing.
    But my issue is a bit extensive i.e. I have a bean which uses a map:

    <bean id="ROUTER" class="com.something.routerImpl"> 
    	<property name="map">
    			<entry key="1" value-ref="bean1" />
    			<entry key="2" value-ref="bean2" />		
    			<entry key="3" value-ref="bean3" />
    			<entry key="4" value-ref="bean4" />			
    Now the map keys will vary by environment - test may have 4 and prod may have 10. And so the number of value-ref beans will also increase in prod. Therefore I can not simply use property file to solve this.