Details of the spring parent child container and configuration

  • 2020-12-10 00:44:19
  • OfStack

spring parent-child container

spring's total context containers are parent-child, parent and child containers. ** Parent containers are visible to child containers, and child containers are not visible to parent containers **.

For traditional spring mvc, the spring mvc container is a child container, that is, the corresponding container of ServletDispatcher is a child container, and the parent container configured in ES12en.xml through the contextConfigLocation attribute of ConextLoaderListener is the parent container.

Usage scenarios for parent-child containers

The primary use of parent-child containers is context isolation. Consider the following scenario.

project-service.jar is the service layer module. Contains 1 database service methods. The corresponding spring configuration file is ES26en-ES27en.xml. project-api is the api server code. It relies on ES32en-ES33en.jar. The corresponding configuration file is ES35en-ES36en.xml.

project-api requires decorate to decorate some of the methods in ES42en-ES43en, such as CustomerService. The decorated class is CachedCustomerService. Thus, project-ES48en now contains two CustomerService, one CustomerService from ES50en-ES51en and one CachedCustomerService. At this point, if all the configuration files for the ES54en-ES55en project are loaded in one context, there is bound to be a problem (the usual practice is to use the import tag to send all import in). Because CustomerService in project is injected via @ES60en standard, similar to the following


@Serivce
public class PayService{
@Resource
private CustomerService cusService;
}

The solution

At this point, since the context is injecting the customerService attribute, two CustomService are encountered. It cannot determine which Service to inject.

Of course, one could say, change the Resource property of PayService to 1 and specify which one to inject. However, project-ES75en.jar is the third repository, and it becomes impossible to change the code unless you get the source code.

At this point, you can solve the problem by using parent-child containers.

project-service is placed in the parent container, and project-ES84en all bean are loaded in the child container.

Assume that the context configuration file for ES88en-ES89en is ES90en-ES91en.xml and do so as follows.

1. Define ES95en-ES96en.xml


 <bean id = "serviceContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
  <constructor-arg>
  <value>
   classpath:project-service.xml
  </value>
  </constructor-arg>
 </bean>

 <bean id = "apiContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
  <constructor-arg>
   <value>
    classpath:project-api.xml
   </value>
  </constructor-arg>

  <constructor-arg>
   <ref bean="serviceContext"/>
  </constructor-arg>
 </bean>

2. In the context configuration of ES101en.xml, as follows.


 <context-param> 
  <param-name>contextConfigLocation</param-name>
  <param-value> classpath*:project-total.xml</param-value> 
 </context-param>

 <listener> 
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
  </listener>

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

serviceContext is the parent container and apiContext is the child container, thus serviceContext cannot see apiContext, while apiContext can see the effect of serviceContext.


Related articles: