Is it possible to add beans to the application context, if a bean is loading?
I want to create a ApplicationContextTemplateFactoryBean that generates a valid ApplicationContext file based on a ApplicationContext template and after the generation, the ApplicationContext should be parsed and added to the current ApplicationContext, just like other application contexts.
With my last few project I have a lot of application context that only differ from some bean names/classnames/values.. but are the same. That is why I want to use a template-based applicationcontext so I can generate concrete applicationcontexts on the fly. I have been looking through the current applicationcontext, but I haven`t figured out a way to load application contextfiles while the app context is loading.
[edit]
This is where I`m at, at the moment
Code:public class ApplicationContextTemplateFactoryBean implements InitializingBean, ApplicationContextAware, ServletContextAware { private Map _propertyMap; private File _templateFile; private ApplicationContext _applicationContext; private ServletContext _servletContext; public Map getPropertyMap() { return _propertyMap; } public void setPropertyMap(Map propertyMap) { _propertyMap = propertyMap; } public File getTemplateFile() { return _templateFile; } public void setTemplateFile(File templateFile) { _templateFile = templateFile; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { _applicationContext = applicationContext; } public void setServletContext(ServletContext servletContext) { _servletContext = servletContext; } private GenericWebApplicationContext createAppContext(String context) { GenericWebApplicationContext factory = new GenericWebApplicationContext(); factory.setParent(_applicationContext); factory.setServletContext(_servletContext); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.setEntityResolver(new ResourceEntityResolver(_applicationContext)); reader.loadBeanDefinitions(new ByteArrayResource(context.getBytes())); factory.refresh(); return factory; } public void afterPropertiesSet() throws Exception { if (_templateFile == null) throw new IllegalArgumentException("templateFile is not set"); if (_propertyMap == null) _propertyMap = new HashMap(); String appContextContent = generateContext(); GenericWebApplicationContext c = createAppContext(appContextContent); } private String generateContext() throws VelocityException { VelocityContext context = new VelocityContext(); for (Iterator<String> it = _propertyMap.keySet().iterator(); it.hasNext();) { String key = it.next(); Object value = _propertyMap.get(key); context.put(key, value); } VelocityEngine engine = new VelocityEngine(); return VelocityEngineUtils.mergeTemplateIntoString(engine, _templateFile.getPath(), _propertyMap); } }


Reply With Quote