Hi,
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:
01 | < bean id = "propertyConfigurer" |
02 | class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > |
03 | < property name = "locations" > |
05 | < value >classpath:file1.properties</ value > |
06 | < value >classpath:file2.properties</ value > |
07 | < value >classpath:file3.properties</ value > |
12 | < import resource = "classpath:huo-spring-business-services.xml" /> |
13 | < import resource = "classpath:huo-spring-datasource.xml" /> |
14 | < import resource = "classpath:huo-spring-security-config.xml" /> |
15 | < import resource = "classpath:huo-spring-security-access.xml" /> |
16 | < import resource = "classpath:huo-spring-wf.xml" /> |
This context is loaded in the web.xml file of web application:
2 | < param-name >contextClass</ param-name > |
3 | < param-value >org.springframework.web.context.support.XmlWebApplicationContext</ param-value > |
7 | < param-name >contextConfigLocation</ param-name > |
8 | < param-value >/WEB-INF/applicationContext.xml</ param-value > |
In our example, we want to filter the last import in the root context:
1 | < import resource = "classpath:huo-spring-wf.xml" /> |
in terms of a properties in the “huo.config.wf.enable” in the properties file “file3.properties”:
1 | huo.config.wf.enable=false |
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:
2 | * Map containing the resources do not load |
4 | private Map<String, Boolean> locationPatternRessourceNOLoad = new HashMap<String, Boolean>(); |
01 | public ConditionalConfigurationXmlWebApplicationContext() { |
05 | InputStream in = null ; |
07 | String name = "file3.properties" ; |
08 | Properties result = null ; |
09 | ClassLoader loader = this .getClassLoader(); |
11 | loader = ClassLoader.getSystemClassLoader (); |
13 | in = loader.getResourceAsStream (name); |
15 | result = new Properties (); |
24 | Object val = result.get( "huo.config.wf.enable" ); |
26 | locationPatternRessourceNOLoad.put( "classpath:huo-spring-wf.xml" , Boolean.parseBoolean(val.toString())); |
29 | } catch (Throwable ignore){ |
31 | if (in != null ) try { in.close (); } catch (Throwable ignore) {} |
02 | * Resolve the given location pattern into Resource objects. |
05 | public Resource[] getResources(String locationPattern) throws IOException{ |
07 | Boolean toLoad = true ; |
08 | if (locationPatternRessourceNOLoad.containsKey(locationPattern)){ |
09 | toLoad = locationPatternRessourceNOLoad.get(locationPattern); |
11 | if (toLoad.booleanValue()){ |
12 | return super .getResources(locationPattern); |
14 | return new Resource[]{}; |
2 | * Méthode appellée et retournant les fichiers paramétrés dans le paramètre "contextConfigLocation" du fichier web.xml |
So, I have this in my web.xml:
02 | < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > |
06 | < param-name >contextClass</ param-name > |
08 | < param-value >huo.ConditionalConfigurationXmlWebApplicationContext</ param-value > |
12 | < param-name >contextConfigLocation</ param-name > |
13 | < param-value >/WEB-INF/applicationContext.xml</ param-value > |
ConditionalConfigurationXmlWebApplicationContext.zip
Source: XmlWebApplicationContext
Best regards,
Huseyin
Related