Loading a Spring configuration file programitically
In some of my previous posts I mentioned that I was using the Java Plugin Framework in a web application. The plugins were both Spring and Struts2 enabled but as the plugins were loaded at runtime I had to load the applicationContext.xml file from the plugin programitically. One of the problems was that the plugin were loaded using a different class loader to the web application. Luckily Spring has a way to work around this already built in:
try {
// Get the current spring config files that were loaded when the server started up
XmlWebApplicationContext xmlWebApplicationContext = (XmlWebApplicationContext) applicationContext;
xmlWebApplicationContext.setClassLoader(pluginClassLoader);
String[] locations = xmlWebApplicationContext.getConfigLocations();
String[] newLocations = new String[locations.length + 1];
for (int i = 0; i < locations.length; i++) {
newLocations[i] = locations[i];
}
// Load our new applicationContext.xml one in with them
newLocations[locations.length] = "file:" + pluginLocation + File.separator + "applicationContext.xml";
logger.debug("Added to locations [" + newLocations[locations.length] + "]");
for (int i = 0; i < newLocations.length; i++) {
logger.debug("newLocations at [" + i + "] is [" + newLocations[i] + "]");
}
// Refresh the spring context with all the spring config files
xmlWebApplicationContext.setConfigLocations(newLocations);
logger.debug("xmlWebApplicationContext bean factory classloader is [" + xmlWebApplicationContext.getBeanFactory().getBeanClassLoader().getClass().getName() + "]");
xmlWebApplicationContext.refresh();
} catch (Exception e) {
throw new Exception("Could not initialise spring component of plugin [" + pluginLocation + "] nested exception is [" + e + "]");
}
02.03.2009 11:44 - Posted by doahh - Comments: 3 - Java

Comments:
Hi Its good to see that you have implemented Java Plugin Framework in your web application.
I am working on project which needs JPF for plugin based application and springs how did you implement the dispatcher servlet of the plugis in the application.
Please give some tips
Thanks a lot.
If you could provide the basic things that i have to do it woukld be of a great help
23.03.2010 12:27 - Posted by Nadar Gunasekar - Permalink
I am not quite sure what you mean by the dispatcher servlet but it was a while ago that I worked on this. The basic method of loading the plugins applicationContext.xml file was something like:
1) Get root XmlWebApplicationContext;
2) create a child XmlWebApplicationContext and load the plugins applicationContext.xml file into it;
3) Set the child XmlWebApplicationContext to have the correct parent;
4) call refresh on the XmlWebApplicationContext.
23.03.2010 08:35 - Posted by doahh - Permalink
Maybe this is useful - it's maybe has bits missing as I have cut it from between other code:
XmlWebApplicationContext rootXmlWebApplicationContext = (XmlWebApplicationContext) applicationContext;
XmlWebApplicationContext pluginXmlWebApplicationContext = new XmlWebApplicationContext();
pluginXmlWebApplicationContext.setServletContext(getServletContext());
pluginXmlWebApplicationContext.setParent(rootXmlWebApplicationContext);
String configLocation = "file:" + pluginLocation + PLUGIN_APPLICATION_CONTEXT_FILE;
String[] configLocations = {configLocation};
pluginXmlWebApplicationContext.setConfigLocations(configLocations);
pluginXmlWebApplicationContext.refresh();
23.03.2010 08:36 - Posted by doahh - Permalink