Spring中父子容器的實現實例Spring的父子容器可以通過ConfigurableApplicationContext或ConfigurableBeanFactory來實現,這兩個接口中分別有setParent及setParentBeanFactory方法,可以與當前的子容器進行父子容器關聯,這個時候子容器就可以引用父容器中的bean,但是父容器是不能夠引用子容器中的bean的,並且各個子容器中定義的bean是互不可見的,這樣也可以避免因為不同的插件定義了相同的bean而帶來的麻煩。應用場景包括插件或組件的接入,只需要對方提供JAR即可,由父容器進行引導,各個子容器再完成自己的應該完成的工作即可。
以下是一個通過ConfigurableApplicationContext來實現的實例。
1、父容器需要的幾個文件:
1)、Runner.java
//這個類負責啟動父容器
public class Runner {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/spring1.xml");
}
}
2)、ParentClass 這個類主要用於測試子容器是否可以獲取父容器中定義的bean
public class ParentClass{
public void print(){
System.out.println("This is parent class.");
}
}
3)、PluginLoader 這個類用於對子容器的加載,建立父子容器關系
public class PluginLoader implements ApplicationContextAware {
ApplicationContext parentApplicationContext;
ConfigurableApplicationContext childContext;
public void load() {
//掃描所有classpath下面的以plugin_開頭spring的配置文件進行裝配,這里約定所有的子容器插件都必須有一個以plugin_開頭的配置文件,並通過這個文件被父容器加載
childContext = new ClassPathXmlApplicationContext("classpath*:/plugin_*.xml");
childContext.setParent(parentApplicationContext);
childContext.refresh();
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.parentApplicationContext = applicationContext;
}
}
4)、spring1.xml
<bean id="parentClass" class="com.test1.ParentClass"></bean>
<bean id="pluginLoader" class="com.test1.PluginLoader" init-method="load"></bean>
簡單的父容器就只需要這么幾個類了。
2、子容器1需要的幾個文件
我准備了兩個子容器,並加了互相測試調用的的測試,看子容器是否可以引用另外一個子窗口中的bean,不過因為兩個子容器的實現完全相同,只是由1改成2就可以了,以下就只貼出子容器1需要的代碼,子容器2的代碼類似。
1)、plugin_1.xml 這個類是約定好必須存在的,由容器進行引導
<pre name="code" class="html"><bean id="childContext1" class="com.test1.child.ChildContext1"></bean>
2)、ChildContext1.java 加載自己容器中所有的配置,並與父容器建立父子容器關系
public class ChildContext1 implements ApplicationContextAware {
ApplicationContext parentApplicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//父容器中沒有建立父子容器關系之前,是獲取不到parent的,只有父容器執行refresh方法后,第二次初使化子容器才會獲取得到
//也就是第一次的初使化就不執行了,等父容器中建立好父子容器關系后再進行初使化,因為子容器需要引用父容器中的parentClass
if(applicationContext.getParent()==null){
return;
}
//Get parent application context
this.parentApplicationContext = applicationContext.getParent();
ConfigurableApplicationContext childContext = new ClassPathXmlApplicationContext("classpath:/child1.xml");
childContext.setParent(this.parentApplicationContext);
childContext.refresh();
ParentClass parentClass = childContext.getBean(ParentClass.class);
Assert.assertNotNull(parentClass);
}
}
3)、child1.xml
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
<bean id="childClass1" class="com.test1.child.ChildClass1">
4)、ChildClass1
public class ChildClass1 implements InitializingBean {
//這里required加上false,是因為是沒有建立父子容器關系之前,這個parentClass是注入不了了的
@Autowired(required = false)
ParentClass parentClass;
//這里required加上false,是因為子容器之前是不能夠相互引用的,只是測試使用。另注:這個類沒有放到這里,在子容器2中,這里不貼代碼了
@Autowired(required = false)
ChildClass2 childClass2;
public void print() {
if (parentClass != null) {
parentClass.print();
}
System.out.println("This is child class 1");
if (childClass2 != null) {
childClass2.print();
}
}
public void afterPropertiesSet() throws Exception {
print();
}
}
再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow