Note: Spring nested ApplicationContext

Keywords: Programming Spring xml Java Attribute

Bean injection and injection are in the same ApplicationContext, so they are the same BeanFactory. However, Spring supports the ApplicationContext hierarchy, so one context and the associated BeanFactory are considered the parent of another. By nesting ApplicationContext, the configuration can be divided into different files, which is a godsend for large projects with many beans.

When nesting Application instances, Spring allows beans in the child context to refer to beans in the parent context. Using GenericXmlApplicationContext to implement ApplicationContext nesting is not difficult to understand. To say that a GenericXmlApplicationContext is nested in another GenericXmlApplicationContext, just call the setParent() method of the subapplicationcontext. The code is as follows:

 public static void main(String[] args) {
        GenericXmlApplicationContext parent = new GenericXmlApplicationContext();
        parent.load("classpath:nest/parent-app-xml.xml");
        parent.refresh();

        GenericXmlApplicationContext child = new GenericXmlApplicationContext();
        child.load("classpath:nest/child-app-xml.xml");
        child.setParent(parent);
        child.refresh();

        Song song1 = child.getBean("song1", Song.class);
        Song song2 = child.getBean("song2", Song.class);
        Song song3 = child.getBean("song3", Song.class);
        System.out.println("from parent ctx: " + song1.getTitle());
        System.out.println("from child  ctx: " + song2.getTitle());
        System.out.println("from parent ctx: " + song3.getTitle());
        child.close();
        parent.close();
    }


//Parent app XML configuration
//In the configuration file of the child ApplicationContext, the working principle of referencing a bean in the parent ApplicationContext is exactly the same as that of referencing a bean in the child ApplicationContext, unless there is a bean with the same name shared in the child ApplicationContext. In this case, you just need to replace the bean attribute of the ref element with parent, and then you can continue to use it.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	         http://www.springframework.org/schema/beans/spring-beans.xsd
		 http://www.springframework.org/schema/context
		 http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="childTitle" class="java.lang.String" c:_0="Daughters"/>
    <bean id="parentTitle" class="java.lang.String" c:_0="Gravity"/>

</beans>

//Child app XML configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	         http://www.springframework.org/schema/beans/spring-beans.xsd
		 http://www.springframework.org/schema/context
		 http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="song1" class="com.source.study.spring.nest.context.Song" p:title-ref="parentTitle"/>
    <bean id="song2" class="com.source.study.spring.nest.context.Song" p:title-ref="childTitle"/>
    <bean id="song3" class="com.source.study.spring.nest.context.Song">
        <property name="title">
            <ref parent="childTitle"/>
        </property>
    </bean>
    <bean id="childTitle" class="java.lang.String" c:_0="No Such Thing"/>
</beans>

//Song class definition
public class Song {

    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

The Song1 bean uses the bean ref attribute to refer to a bean named parentTitle. Because this exists in the parent BeanFactory, song1 receives the reference to the bean.

The Song2 bean references childTitle. Because the bean exists in two ApplicationContext, it receives the childTitle in its ApplicationContext.

The Song3 bean references the childTitle declared in the parent ApplicationContext, so instances in the child ApplicationContext are ignored.

Result output:

Posted by Iceman18 on Fri, 17 Apr 2020 08:51:47 -0700