Configure each extension in a separate application context. In each application context, define a BeanPostProcessor which saves every bean the application context defines to a registry that maps beans to application contexts.
Configure an application context which will be the parent for each extension application context. In this configuration file, define a bean which maps bean names to application context identifiers.
<!-- parent.xml -->
<beans>
<bean
id="beanNameToApplicationContextIdMap"
class="java.util.HashMap"/>
</beans>
The extension application context configuration file defines an instance of BeanNameToApplicationContextIdMapInserter
, which is a custom class implementing the BeanPostProcessor
interface. The applicationContextId
property is configured to a string identifying the application context. The map
property configures the instance to update the map defined in the parent application context.
<!-- alpha.xml -->
<beans>
<bean class="com.example.BeanNameToApplicationContextIdMapInserter">
<property name="applicationContextId" value="alpha"/>
<property name="map" ref="beanNameToApplicationContextIdMap"/>
</bean>
<bean id="alphaService" class="...">
</bean>
</beans>
The BeanNameToApplicationContextIdMapInserter
source code:
public class BeanNameToApplicationContextIdMapInserter implements BeanPostProcessor {
private String applicationContextId;
private HashMap<String, String> map;
public void setApplicationContextId(String id) {
this.applicationContextId = id;
}
public void setMap(HashMap<String, String> map) {
this.map = map;
}
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException
{
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException
{
map.put(beanName, applicationContextId);
return bean;
}
}
You can use SingletonBeanFactoryLocator to configure the parent application context and each extension application context.
<!-- beanRefFactory.xml -->
<beans>
<bean
id="parentApplicationContext"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<value>parent.xml</value>
</constructor-arg>
</bean>
<bean
id="alphaApplicationContext"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<value>alpha.xml</value>
</constructor-arg>
<constructor-arg>
<ref bean="parentApplicationContext"/>
</constructor-arg>
</bean>
<bean
id="bravoApplicationContext"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<value>bravo.xml</value>
</constructor-arg>
<constructor-arg>
<ref bean="parentApplicationContext"/>
</constructor-arg>
</bean>
</beans>
Here is example code that reads the bean to application context map.
public class Main {
private static ApplicationContext getApplicationContext(String name) {
BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
BeanFactoryReference bfr = bfl.useBeanFactory(name);
return (ApplicationContext) bfr.getFactory();
}
public static void main(String[] args) {
ApplicationContext parentApplicationContext =
getApplicationContext("parentApplicationContext");
HashMap<String, String> map = (HashMap<String, String>)
parentApplicationContext.getBean("beanNameToApplicationContextIdMap");
System.out.println(map.get("alphaService"));
System.out.println(map.get("bravoService"));
}
}