Why are spring and Spring MVC package scans separate?

Source: Internet
Author: User

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
  1. <!--spring configuration file--
  2. <context:component-scan base-package="Com.xxx.xxx.account.front">
  3. <context:exclude-filter type="annotation"
  4. expression="Org.springframework.stereotype.Controller" />
  5. </context:component-scan>
  6. <!--spring MVC--
  7. <context:component-scan base-package="Com.xxx.xxx.account.front.web" use-default-filters= "False">
  8. <context:include-filter type="annotation"
  9. expression="Org.springframework.stereotype.Controller" />
  10. </context:component-scan>

Test Bean

Java code
  1. @Service
  2. Public class Testservice implements Initializingbean {
  3. @Autowired
  4. private Personaladdressajaxcontroller Personaladdressajaxcontroller;
  5. @Override
  6. public void Afterpropertiesset () throws Exception {
  7. System.out.println ("--------------------------------");
  8. }
  9. }

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
  1. <!--spring configuration file--
  2. <context:component-scan base-package="Com.xxx.xxx.account.front">
  3. </context:component-scan>
  4. <!--spring MVC--
  5. <context:component-scan base-package="Com.xxx.xxx.account.front.web" use-default-filters= "False">
  6. <context:include-filter type="annotation"
  7. expression="Org.springframework.stereotype.Controller" />
  8. </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
    1. <!--spring configuration file--
    2. <context:component-scan base-package="Com.xxx.xxx.account.front">
    3. </context:component-scan>
    4. <!--spring MVC--
    5. 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
  1. <!--spring configuration file--
  2. <context:component-scan base-package="Com.xxx.xxx.account.front">
  3. <context:exclude-filter type="annotation"
  4. expression="Org.springframework.stereotype.Controller" />
  5. </context:component-scan>
  6. <!--spring MVC--
  7. <context:component-scan base-package="Com.xxx.xxx.account.front.web" use-default-filters= "False">
  8. <context:include-filter type="annotation"
  9. expression="Org.springframework.stereotype.Controller" />
  10. </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
    1. <!--spring configuration file--
    2. No
    3. <!--spring MVC--
    4. <context:component-scan base-package="Com.xxx.xxx.account.front.web" use-default-filters= "True">
    5. </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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.