spring parent-child container
The general context container of spring can be divided into father and son, parent container and child container. ** The parent container is visible to the child container, and the child container is not visible to the parent container**.
For traditional spring mvc, the spring mvc container is a child container, that is, the container corresponding to ServletDispatcher is a child container, and the configuration in web.xml is the parent container through the contextConfigLocation property of the ConextLoaderListener.
Use scenarios of father and son containers
The main purpose of the father-son container is context isolation. Consider one of the following scenarios.
project-api needs to decorate some methods in project-service and decorate them, such as decorating CustomerService. The decorated class is CachedCustomerService. So, now the project-api contains two CustomerServices, one is the CustomerService from the project-service, and the other is the CachedCustomerService. At this time, if all the configuration files of the project-api project are loaded through a context, problems will inevitably arise (the usual practice is to use the import tag to import them all). Because, the PayService in the project is injected into the CustomerService through the @Resource standard, similar to the following
@Serivcepublic class PayService{@Resourceprivate CustomerService cusService;}Solution
At this time, because the context injects the customerService property, two CustomServices are encountered. It cannot interpret which service to inject.
Of course, some people will say, change the Resource property of PayService and specify which one to inject. However, if project-service.jar is a third-party library, changing the code becomes unfeasible unless the source code is obtained.
At this time, this problem can be solved through the parent-son container.
Put the project-service in the parent container, and all project-api beans are loaded in child containers.
Assuming that the context configuration file of project-api is project-api.xml, the implementation method is as follows.
1. Define project-total.xml
<bean id = "serviceContext"> <constructor-arg> <value> classpath:project-service.xml </value> </constructor-arg> </bean> <bean id = "apiContext"> <constructor-arg> <value> classpath:project-api.xml </value> </constructor-arg> <constructor-arg> <ref bean="serviceContext"/> </constructor-arg> </bean>
2. In the context configuration of web.xml, the following is.
<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, so that the serviceContext cannot see the apiContext, and apiContext can see the effect of serviceContext.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.