What is the difference between spring parent context and child context?

I read the spring doc main container. I want to understand the purpose of ref parent when introducing co-authors, then I found this concept of parent context child context or parent container and current container, this is the part that I confuse about this: This part of the document

Specifying the target bean through the parent attribute creates a reference to the bean that is in the parent container of the current container. The value of the parent attribute can be the same as either the id attribute of the target bean or one of the values ​​in the name of the target bean attribute, and the target bean must be in the parent container of the current one. You use this reference bean option mainly when you have a container hierarchy and you want an existing bean in the parent container with the proxy server, which will have the same name as the parent bean.

<!-- in the parent context --> <bean id="accountService" class="com.foo.SimpleAccountService"> <!-- insert dependencies as required as here --> </bean> <!-- in the child (descendant) context --> <bean id="accountService" <!-- bean name is the same as the parent bean --> class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref parent="accountService"/> <!-- notice how we refer to the parent bean --> </property> <!-- insert other configuration and dependencies as required here --> </bean> 

can someone give me some help or an example of these two types of context? and what is the purpose of ref parent . Thanks in advance.

+5
source share
1 answer

Spring beans run inside the application context.

The application context is the Spring Advanced Container. Similar to BeanFactory, it can load bean definitions, wire beans together, and distribute beans upon request. In addition, it adds more corporate features, such as the ability to resolve text messages from a properties file and the ability to publish applications for interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface. https://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm

For each application context, you can have many configuration files, configuration classes, or a combination of both.

You can create an application context with this code:

 ApplicationContext context = new FileSystemXmlApplicationContext("Beans.xml"); 

And we get beans with context.getBean or with @autowired .

There are some cases when you want (or need) to have a context hierarchy. In this case, Spring provides a way to specify the parent context. If you look at this constructor, you will see that it gets the context parent.

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/FileSystemXmlApplicationContext.html#FileSystemXmlApplicationContext-org.springframework.context.ApplicationContext-

As you can see, the parent context is the same type of child context, they are both http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html .

The difference is that they are linked through the parent / child relationship. Do not compose (import) relationships.

The most common case when you see this in a Spring MVC application, these applications have 2 contexts, frist is the dispatcher servlet context, and the other is the root context. Here you can see the relationship http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-servlet

And here you can see an example of the application context hierarchy in Spring boot applications.

https://dzone.com/articles/spring-boot-and-application-context-hierarchy

+3
source

Source: https://habr.com/ru/post/1266817/


All Articles