views:

3409

answers:

5

In my Spring xml configuration I'm trying to get something like this to work:

<beans>

   <import resource="${file.to.import}" />

   <!-- Other bean definitions -->

</beans>

I want to decide which file to import based on a property in a properties file. I know that I can use a System property, but I can't add a property to the JVM at startup.

Note: The PropertyPlaceHolderConfigurer will not work. Imports are resolved before any BeanFactoryPostProcessors are run. The import element can only resolve System.properties.

Does anyone have a simple solution to this? I don't want to start subclassing framework classes and so on...

Thanks

+1  A: 

Why not:

  1. read your properties file on startup
  2. that will determine which Spring config to load
  3. whichever Spring config is loaded sets specific stuff, then loads a common Spring config

so you're effectively inverting your current proposed solution.

Brian Agnew
The entrypoint to the application is an EJB which loads the ApplicationContext on the first method call, so I can't really do what you're suggesting.
nash2000
+1  A: 

Add something similar to the following:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound"><value>true</value></property>
    <property name="locations">
        <list>
            <value>classpath:propertyfile.properties</value>
        </list>
    </property>
</bean>
laz
Thanks, but the PropertyPlaceHolderConfigurer will not work :)
nash2000
Bummer. I see the note you added about the ordering of post processors and imports.
laz
A: 

This is, unfortunately, a lot harder than it should be. In my application I accomplished this by doing the following:

  1. A small, "bootstrap" context that is responsible for loading a PropertyPlaceholderConfigurer bean and another bean that is responsible for bootstrapping the application context.

  2. The 2nd bean mentioned above takes as input the "real" spring context files to load. I have my spring context files organized so that the configurable part is well known and in the same place. For example, I might have 3 config files: one.onpremise.xml, one.hosted.xml, one.multitenant.xml. The bean programmatically loads these context files into the current application context.

This works because the context files are specified as input the the bean responsible for loading them. It won't work if you just try to do an import, as you mentioned, but this has the same effect with slightly more work. The bootstrap class looks something like this:

 public class Bootstrapper implements ApplicationContextAware, InitializingBean {

    private WebApplicationContext context;
    private String[] configLocations;
    private String[] testConfigLocations;
    private boolean loadTestConfigurations;

    public void setConfigLocations(final String[] configLocations) {
        this.configLocations = configLocations;
    }

    public void setTestConfigLocations(final String[] testConfigLocations) {
        this.testConfigLocations = testConfigLocations;
    }

    public void setLoadTestConfigurations(final boolean loadTestConfigurations) {
        this.loadTestConfigurations = loadTestConfigurations;
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        context = (WebApplicationContext) applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String[] configsToLoad = configLocations;

        if (loadTestConfigurations) {
            configsToLoad = new String[configLocations.length + testConfigLocations.length];
            arraycopy(configLocations, 0, configsToLoad, 0, configLocations.length);
            arraycopy(testConfigLocations, 0, configsToLoad, configLocations.length, testConfigLocations.length);
        }

        context.setConfigLocations(configsToLoad);
        context.refresh();
    }
}

Basically, get the application context, set its config locations, and tell it to refresh itself. This works perfectly in my application.

Hope this helps.

lrm
Note: This code was taken from a working system, but I scrubbed it manually of some small bits and pieces related to the system itself. Apologize if there are any errors.
lrm
Thanks, but unfortunately I don't have complete control of the startup as an EJB is the entrypoint to the application. Maybe I should just upgrade to Spring 3 and use the JavaConfig instead :)
nash2000
I don't think you need any control other than being able to tell the EJB which spring context to load. Am I missing something?
lrm
A: 

If what you want is to specify the imported XML file name outside applicationContext.xml so that you could replace applicationContext.xml without losing the configuration of the imported XML file path, you can just add an intermediate Spring beans XML file, say, confSelector.xml, so that applicationContext.xml imports confSelector.xml and confSelector.xml only contains an <import> element that refers to the suitable custom beans XML file.

Another means that might be of use are XML entities (defined by adding <!ENTITY ... > elements into the DTD declaration at the beginning of XML). These allow importing XML fragments from other files and provide "property placeholder"-like functionality for any XML file.

Neither of these solutions allows you to have the configuration file in Java's .properties format, though.

Jaan
A: 

There is an old issue on the Spring JIRA for adding properties placeholder support for import (SPR-1358) that was resolved as "Won't Fix", but there has since been a proposed solution using an EagerPropertyPlaceholderConfigurer.

I've been lobbying to have SPR-1358 reopened, but no response so far. Perhaps if others added their use cases to the issue comments that would help raise awareness.

Ian Brandt