I will expose you a solution implemented in order to:
– filter imports/resource loading in a spring context of general applicationContext.xml,
– filter resources in imports or even overloading of beans,
– test the environment and conditionally add path locations,

For our example, in an web application with a root context definition applicationContext.xml:

	<bean id="propertyConfigurer"
		<property name="locations">
	<import resource="classpath:huo-spring-business-services.xml" />
	<import resource="classpath:huo-spring-datasource.xml" />
	<import resource="classpath:huo-spring-security-config.xml" />
	<import resource="classpath:huo-spring-security-access.xml" />
	<import resource="classpath:huo-spring-wf.xml" />

This context is loaded in the web.xml file of web application:



In our example, we want to filter the last import in the root context:

<import resource="classpath:huo-spring-wf.xml" />

in terms of a properties in the “huo.config.wf.enable” in the properties file “file3.properties”:


So, we have create a new class ConditionalConfigurationXmlWebApplicationContext extending the Spring standard XmlWebApplicationContext. This new class can override the “refresh()”, “getConfigLocations()” or “getResources()” methods, where it will test its environment and possibly modify the “configLocations”.

Important note: To be catched by our above class, we nedd to add “classpath:” in all imports like ‘import resource=”classpath:spring-except.xml” ‘.

For our example and needs, it would do something like:

	 * Map containing the resources do not load
	private Map<String, Boolean> locationPatternRessourceNOLoad = new HashMap<String, Boolean>();
	public ConditionalConfigurationXmlWebApplicationContext() {
		// Exemple de filtrage wf
	        InputStream in = null;
				String name = "file3.properties";
		        Properties result = null;
		        ClassLoader  loader = this.getClassLoader();
		        if (loader == null){
		        	loader = ClassLoader.getSystemClassLoader ();
	            in = loader.getResourceAsStream (name);
	            if (in != null){
                    result = new Properties ();
					result.load(in); // Can throw IOException
	            //for (Iterator iterator = result.keySet().iterator(); iterator.hasNext();) {
	            //	Object key = iterator.next();
				//	Object value = result.get(key);
				//	System.out.println(key+"="+value);
	            Object val = result.get("huo.config.wf.enable");
	            if(val != null){
           			locationPatternRessourceNOLoad.put("classpath:huo-spring-wf.xml", Boolean.parseBoolean(val.toString()));

			}catch (Throwable ignore){
	            if (in != null) try { in.close (); } catch (Throwable ignore) {}
	 * Resolve the given location pattern into Resource objects. 
	public Resource[] getResources(String locationPattern) throws IOException{
			Boolean toLoad = true;
				toLoad = locationPatternRessourceNOLoad.get(locationPattern);
			if(toLoad.booleanValue()){ // LOAD
				return super.getResources(locationPattern);
			}else{ // NO LOAD
				return new Resource[]{};
	 * Méthode appellée et retournant les fichiers paramétrés dans le paramètre "contextConfigLocation" du fichier web.xml 
	public String[] getConfigLocations() {
		//locations = append(locations, "classpath*:/META-INF/config/service-module-test-only.xml");
		return super.getConfigLocations();

So, I have this in my web.xml:


		<!-- <param-value>org.springframework.web.context.support.XmlWebApplicationContext</param-value> -->



Source: XmlWebApplicationContext

Best regards,