Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Configure each extension in a separate application context. In each application context, define a <a href="http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-extension-bpp" rel="nofollow noreferrer">BeanPostProcessor</a> which saves every bean the application context defines to a registry that maps beans to application contexts.</p> <p>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.</p> <pre><code>&lt;!-- parent.xml --&gt; &lt;beans&gt; &lt;bean id="beanNameToApplicationContextIdMap" class="java.util.HashMap"/&gt; &lt;/beans&gt; </code></pre> <p>The extension application context configuration file defines an instance of <code>BeanNameToApplicationContextIdMapInserter</code>, which is a custom class implementing the <code>BeanPostProcessor</code> interface. The <code>applicationContextId</code> property is configured to a string identifying the application context. The <code>map</code> property configures the instance to update the map defined in the parent application context.</p> <pre><code>&lt;!-- alpha.xml --&gt; &lt;beans&gt; &lt;bean class="com.example.BeanNameToApplicationContextIdMapInserter"&gt; &lt;property name="applicationContextId" value="alpha"/&gt; &lt;property name="map" ref="beanNameToApplicationContextIdMap"/&gt; &lt;/bean&gt; &lt;bean id="alphaService" class="..."&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>The <code>BeanNameToApplicationContextIdMapInserter</code> source code:</p> <pre><code>public class BeanNameToApplicationContextIdMapInserter implements BeanPostProcessor { private String applicationContextId; private HashMap&lt;String, String&gt; map; public void setApplicationContextId(String id) { this.applicationContextId = id; } public void setMap(HashMap&lt;String, String&gt; 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; } } </code></pre> <p>You can use <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/access/SingletonBeanFactoryLocator.html" rel="nofollow noreferrer">SingletonBeanFactoryLocator</a> to configure the parent application context and each extension application context.</p> <pre><code>&lt;!-- beanRefFactory.xml --&gt; &lt;beans&gt; &lt;bean id="parentApplicationContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"&gt; &lt;constructor-arg&gt; &lt;value&gt;parent.xml&lt;/value&gt; &lt;/constructor-arg&gt; &lt;/bean&gt; &lt;bean id="alphaApplicationContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"&gt; &lt;constructor-arg&gt; &lt;value&gt;alpha.xml&lt;/value&gt; &lt;/constructor-arg&gt; &lt;constructor-arg&gt; &lt;ref bean="parentApplicationContext"/&gt; &lt;/constructor-arg&gt; &lt;/bean&gt; &lt;bean id="bravoApplicationContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"&gt; &lt;constructor-arg&gt; &lt;value&gt;bravo.xml&lt;/value&gt; &lt;/constructor-arg&gt; &lt;constructor-arg&gt; &lt;ref bean="parentApplicationContext"/&gt; &lt;/constructor-arg&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>Here is example code that reads the bean to application context map.</p> <pre><code>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&lt;String, String&gt; map = (HashMap&lt;String, String&gt;) parentApplicationContext.getBean("beanNameToApplicationContextIdMap"); System.out.println(map.get("alphaService")); System.out.println(map.get("bravoService")); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload