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 &#40;Iterator<String> it = _propertyMap.keySet&#40;&#41;.iterator&#40;&#41;; it.hasNext&#40;&#41;;&#41; &#123;
			String key = it.next&#40;&#41;;
			Object value = _propertyMap.get&#40;key&#41;;
			context.put&#40;key, value&#41;;
		&#125;

		VelocityEngine engine = new VelocityEngine&#40;&#41;;
		return VelocityEngineUtils.mergeTemplateIntoString&#40;engine, _templateFile.getPath&#40;&#41;, _propertyMap&#41;;
	&#125;
&#125;