Why the Spring parent container is not visible to the bean in the Spring MVC child container, while the child container is visible to the bean in the parent container

screenshot of the article comes from: Spring-SpringMVC parent-son container & AOP uses summary link description


similar to classloader class loading


similar to the idea of inheritance, the methods and properties of the parent class can be inherited and accessed by the child. But the unique properties and methods of the child are not accessible to the father.

in general:
the Bean of the data source, the service layer, the DAO layer, and the transaction is stored in the parent context container. The Bean of the Mvc-related controller is stored in the
subcontext container.
for example, controller needs to use a service in the parent container, and there is an invocation relationship, so it is designed to require a bean that can access the parent container.
the bean in the parent container does not need to use the bean, of the child container. There is no need to design that the parent container can access the child container.

if you are a writer in spring, you can design the parent container to access the child container, but doing so is unnecessary and confusing.

this does not need to be analyzed from the code. The logic is simple.
1. If you can't find bean, in the child container, go to the parent container.
2. It ends if you can't find bean, in the parent container.


first of all, our ssm project basically has the following configuration files:
clipboard.png
spring.xml is the parent container configuration file, and spring-mvc.xml is the child container configuration file.
We can configure `< context:component-scan base-package= "com.blog.service.impl" / >
` scan service package in spring.xml . This belongs to the spring container.
also configures < context:component-scan base-package= "com.blog.controller" / > scan the controller package in spring-mvc.xml , which belongs to the springmvc container.

< hr >
the child container is visible to the bean in the parent container
that is, the child container can access the bean in the parent container
so we add the configuration in spring to the spring-mvc.xml:
at this time there is
in the spring-mvc configuration.
<context:component-scan base-package="com.blog.controller"/>
<context:component-scan base-package="com.blog.service.impl"/>

spring.xml deletes the configuration of the scan package. Start tomcat, access, no problem.

< hr >
parent container verifies that the bean in the child container is not visible
. Put the configuration `< context:component-scan base-package= "com.blog.controller" / >
` that scans Controller in spring-mvc.xml into spring.xml, launch and access, and will prompt 404, indicating that spring (parent container) does not configure controller bean in sprngmvc (child container).
< hr >

so in practical application, we will be responsible for scanning all packages in spring-mvc.xml < context:component-scan base-package= "com.blog.*" / > , rather than configuring scanning in spring.xml

.
Menu