Background:
recently in the construction of new projects found that some spring configuration is not very understanding, such as the spring configuration is clearly configured Component-scan, why the spring MVC configuration file needs to be configured, so it is not superfluous? As a result of the previous direct development in the existing engineering or other engineering configuration files directly copied over, so also did not pay much attention to this issue. Out of curiosity, Google has discovered that the original is a great learning, see below for details. The normal code is as follows:
XML code
- <!--spring configuration file--
- <context:component-scan base-package="Com.xxx.xxx.account.front">
- <context:exclude-filter type="annotation"
- expression="Org.springframework.stereotype.Controller" />
- </context:component-scan>
- <!--spring MVC--
- <context:component-scan base-package="Com.xxx.xxx.account.front.web" use-default-filters= "False">
- <context:include-filter type="annotation"
- expression="Org.springframework.stereotype.Controller" />
- </context:component-scan>
Test Bean
Java code
- @Service
- Public class Testservice implements Initializingbean {
- @Autowired
- private Personaladdressajaxcontroller Personaladdressajaxcontroller;
- @Override
- public void Afterpropertiesset () throws Exception {
- System.out.println ("--------------------------------");
- }
- }
principle:
Originally spring is the parent container, Spring MVC is a child container, the child container can access the bean of the parent container, and the parent container cannot access the bean of the child container.
Specific reference:
A preliminary glimpse of Spring and SPRINGMVC parent-child container relationship
Why does spring not do global package scanning
Analysis of the container relationship between spring and SPRINGMVC
Test One: Spring Load all BEAN,MVC load controller
XML code
- <!--spring configuration file--
- <context:component-scan base-package="Com.xxx.xxx.account.front">
- </context:component-scan>
- <!--spring MVC--
- <context:component-scan base-package="Com.xxx.xxx.account.front.web" use-default-filters= "False">
- <context:include-filter type="annotation"
- expression="Org.springframework.stereotype.Controller" />
- </context:component-scan>
test Result: Testservice through, the interface shows normal.
Cause: The parent container loads all the beans, so the service can access the controller. The MVC container finds the current container by default, and can find the controller rules that are forwarded so the interface jumps normally.
Test Two: Spring loads all BEAN,MVC containers without loading
XML code
- <!--spring configuration file--
- <context:component-scan base-package="Com.xxx.xxx.account.front">
- </context:component-scan>
- <!--spring MVC--
- No
Test result: Testservice through, interface display 404.
Cause: The parent container loads all the beans, so the service can access the controller. The MVC container defaults to finding the controller for the current container, so the interface appears 404.
Test Three: spring loads all except for the controller. BEAN,MVC Load Controller only
XML code
- <!--spring configuration file--
- <context:component-scan base-package="Com.xxx.xxx.account.front">
- <context:exclude-filter type="annotation"
- expression="Org.springframework.stereotype.Controller" />
- </context:component-scan>
- <!--spring MVC--
- <context:component-scan base-package="Com.xxx.xxx.account.front.web" use-default-filters= "False">
- <context:include-filter type="annotation"
- expression="Org.springframework.stereotype.Controller" />
- </context:component-scan>
Test Result: Testservice initialization failed, if the bean is commented out, the interface is normal.
Cause: The parent container cannot access the child container's bean.
Test Four: Spring does not load BEAN,MVC load all the beans
XML code
- <!--spring configuration file--
- No
- <!--spring MVC--
- <context:component-scan base-package="Com.xxx.xxx.account.front.web" use-default-filters= "True">
- </context:component-scan>
test Result: Testservice through, the interface is normal.
Cause: Because all the beans are in a child container, you can also find the controller in the current container, so there is no problem.
question one: Does the singleton Bean have an instance or two instances in the parent-child container?
A: Initialize two times, the Spring container initializes the BEAN,MVC container and initializes the bean, so it should be two beans.
question two: Why not all the beans are scanned in the sub-container?
A: Many articles on the web say that sub-containers do not support AOP, in fact, this is not true. AOP is implemented because the relevant configuration of AOP is normally configured in the spring container, and if all the beans are migrated to the MVC configuration file, all of them are in a child container, which is equivalent to only one container. Disadvantage is not conducive to expansion.
Why are spring and Spring MVC package scans separate?