I have expanded GenericXmlContextLoader
public class MyContextLoader extends GenericXmlContextLoader {
and overrote
protected String[] generateDefaultLocations(Class<?> clazz)
a method for collecting the names of the directory configuration files, which I can specify using SystemProperty (-Dtest.config =).
I also changed the following method to NOT change any locations.
@Override protected String[] modifyLocations(Class<?> clazz, String... locations) { return locations; }
I use this context loader as follows
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = MyContextLoader.class) public class Test { .... }
Running a test using SystemProperty, indicating the source of the configuration files, now allows you to use completely different configurations.
Using SystemProperty is, of course, only one strategy for specifying a configuration location. You can do whatever you want in generateDefaultLocations() .
EDIT:
This solution allows you to use complete various configurations of the application context (for example, for layouts), and not just different properties. You do not need a build step to deploy everything in your "classpath" location. My particular implementation also used the default username to search for the configuration directory (src / test / resources / {user}) if the system property was not set (makes it easier to support specific test environments for all project developers).
Using the PropertyPlaceholder is still possible and recommended.
EDIT
Spring Version 3.1.0 will support XML profiles / environments An abstraction that is similar to my solution and will allow you to select configuration files for different environments / profiles.
Frvabe
source share