Why does the service layer not do global package scan when integrating spring?

Source: Internet
Author: User

Reprint: http://blog.csdn.net/s740556472/article/details/54879954, Spring and SPRINGMVC parent-child container relationship


1. Before you tell a question, understand a relationship.


In general, in our two frameworks for integrating spring and SPRINGMVC, the Web. Xml reads:

  <!--loading Spring containers--<!--initialize the various configuration files that load Application.xml--<Context-param><Param-name>contextconfiglocation</Param-name><Param-value>classpath:spring/application-*.xml</Param-value></Context-param><Listener><Listener-class>org.springframework.web.context.contextloaderlistener</Listener-class></Listener><!--Configuring the SPRINGMVC front-end controller-<Servlet><Servlet-name>taotao-manager</Servlet-name><Servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!--contextconfiglocation is not required, If you do not configure Contextconfiglocation, SPRINGMVC's configuration file defaults to: Web-inf/servlet name+ "-servlet.xml"-- <init-param> <param-name> Contextconfiglocation</param-name> <param-value>classpath:spring/springmvc.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </ SERVLET>              
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22


The first configuration is the initialization of the spring container to load the application file, then the SPRINGMVC Front Controller (dispatchservlet), which creates a new container in the spring container when the Dispatchservlet is configured. This is actually two containers, spring as a parent container, and SPRINGMVC as a child container.

Let's take a look at the principle of this parent-child relationship.


Usually we inject relationships in the project in this order (in conjunction with the diagram): Inject DAO into the service (initialize automatic injection, take advantage of @autowired), then inject service into the controller (initialize auto-inject, use @autowired), looking at the diagram, this means that the child container as SPRINGMVC is accessible to the parent container spring object.


Then ask everyone a question. If it turns out, will you be able to inject the controller into the service?
It's definitely not going to work! (This also indicates that the parent container cannot invoke the child container object.)


If Dao,serive,controller are all in the spring container, the question above is certainly true, because it is in a bean, in a container.


2. Question: Why can't I configure a global scan in the service tier in spring?

For example: The name of my total project in a project is called Com.shop, we are in the configuration Applicationcontext-service.xml, the package scan code is as follows:

<?xml version= "1.0" Encoding= "UTF-8"? ><beans  Xmlns= "Http://www.springframework.org/schema/beans" xmlns : Context= "Http://www.springframework.org/schema/context"  .../omitted here > <!--Scan Package Service implementation Class--<context:component-scan base-package=" Com.shop.service "></context:component-scan></BEANS>         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8


The above configuration is a local scan, not a global scan. Next, the reason:

This is related to the parent-child container mentioned above, assuming we do a global scan then the code is as follows:

<?xml version= "1.0" Encoding= "UTF-8"? ><beans  Xmlns= "Http://www.springframework.org/schema/beans" xmlns : Context= "Http://www.springframework.org/schema/context"  .../omitted here > <!--Scan Package Service implementation Class--<context:component-scan base-package=" Com.shop "></context:component-scan> </BEANS>           
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9


At this time the spring container will be scanned to @controller, @Service, @Reposity, @Component, at this time the figure is as follows



In conjunction with the diagram to see, the equivalent of they will be placed in a large container, and at this time there is no object in the Springmvc container, no object There is no controller, so load the processor, the adapter will not find the mapping object, mapping relations, As a result, a 404 error will appear on the page.


3. If you do not use the spring container, put all the layers directly into the SPRINGMVC container?


Of course, if there is no spring container, we can put all the layers into SPRINGMVC. It is perfectly possible to use this container alone, and it is lightweight.


4. So why do we use the spring container and the SPRINGMVC container together in the project?

The answer is: Spring Extensibility, if the project needs to join struts, etc. can be integrated to facilitate the expansion of the framework. If for quick, for the convenience of development, can use SPRINGMVC frame completely.


5. Conclusion

If we do a global package scan at the service layer in the project, then SPRINGMVC cannot provide the service because there is no controller object in the SPRINGMVC child container.

Why does the service layer not do global package scan when integrating spring?

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.