Dynamically Importing Spring Context Files

We needed to change the underlying storage mechanism (JPA2/JCR/inMemory…) dynamically from configuration and actually the neat and perfect solution for our problem is Spring Profiles from Spring 3.1 as you can see from this excellent post by Chris Beams. But since the beans with Profiles are not there yet and the Spring 3.1 GA release is planned for September 2011, here is another solution using import tag to dynamically load the spring context files depending on the active storage type:

	<import resource="${repository.type.config}"/>

Unfortunately the import tags are not resolving .properties files, so you can only use them with system properties since the imports work before bean initialization as mentioned here. If you want to define/add the system properties with Spring here is a suggested solution but as I tried this would not work as easy as expected since the system properties are not loaded before the importing. So if you want to add system properties from .properties file at Spring startup, you can extend the ContextLoaderListener and override the contextInitialized method to merge the properties from .properties file that you defined as context init parameter in your web.xml.

 

 

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)