Announcement Announcement Module
No announcement yet.
Dynamic Spring Profile Initialization Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic Spring Profile Initialization

    I've written a class that chooses which spring profile to activate based upon externally provided criteria. I define the bean that does this in xml configuration, and want to have that choose the profile before the subsequent imports are executed. Take the following xml fragment:

    HTML Code:
        <bean id="lsDAOProfileActivator" class="" init-method="init">
            <constructor-arg name="pathTemplate" value="config/dao/profiles/%s/"/>
        <!-- Import dao resources, only will activate when the default profile is active.
             Do this for testing actual connections -->
        <import resource="dao-connection-test-live.xml"/>
        <!-- Import mocked versions of the daos.  This is by default when the build is executing without network access
             to the database servers -->
        <import resource="dao-connection-test-mock.xml"/>
    Each of the imports has a profile attribute. The activator bean above it is supposed to choose a profile, which would then be the active profile during the import. The debug output as the program executes shows clearly that the activator bean is not even initialized until all the imports have completed, which for me is too late.

    Any suggestions on what I could do?

  • #2
    I've investigated some more and found that my LSProfileActivator class could be implemented as a ApplicationContextInitializer. This is good, but for the life of me I can't find any way to pass it parameters. It needs the pathTemplate setting in order for me to load properties from the right location. I'm going to try something now and hard-code the location and see if it works to change the active profile, but I need to parameterize this.

    So what I've done now is taken out the bean definition above (id="lsDAOProfileActivator") and annotated the unit test class with:

    @ContextConfiguration(initializers = LSProfileActivator.class)
    Thoughts on how I could pass parameters to LSProfileActivator when it is an ApplicationContextInitializer?