I'd like to make a bean, let's call it a DynamicContextReader, that would use some non-spring config information to determine the names of additional spring XML files to load and then load then dynamically INTO THE SAME WEB APPLICATION CONTEXT it's being loaded from.
I'm having a little difficulty doing this. I tried invoking loadBeanDefinitions() and refresh() from within my DynamicContextReader's init method, but ran into a ConcurrentModificationException, presumably because i was trying to load bean defs while I was already loading bean defs
I then tried to load these XML files into their own individual GenericWebApplicationContext, but then my DispatcherServlet bean (instantiated in one of these dynamically determined files) failed to initialize because it insists on being run in the original WebApplicationContext, not the GenericWebApplicationContext I had just created:
Here's the (simplified) code I used to do this from within my little DynamicContextReader bean:Code:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'collectorServlet' defined in class path resource [collector-context.xml]: Initialization of bean failed; nested exception is org.springframework.context.ApplicationContextException: Cannot reinitialize with different application context: current one is [org.springframework.web.context.support.XmlWebApplicationContext: display name [Root WebApplicationContext]; startup date [Fri Jan 12 16:09:25 PST 2007]; root of context hierarchy; config locations [/WEB-INF/applicationContext.xml]], passed-in one is [org.springframework.web.context.support.GenericWebApplicationContext: display name [org.springframework.web.context.support.GenericWebApplicationContext;hashCode=14794804]; startup date [Fri Jan 12 16:09:25 PST 2007]; root of context hierarchy]
But of course it doesn't work. Is it possible to use a spring bean to dynamically load additional xml files into an existing XmlWebApplicationContext?Code:public void init() { logger.info("Loading bean definitions from " + resource ); GenericWebApplicationContext factory = new GenericWebApplicationContext( (DefaultListableBeanFactory) applicationContext.getBeanFactory() ); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader( factory ); reader.loadBeanDefinitions(resource); factory.refresh(); }
Thanks
Mike



Reply With Quote
