On the inheritance relationship of Spring MVC's Webapplicationcontext

Source: Internet
Author: User

The inheritance relationship of Root/child webapplicationcontext in spring MVC

In the traditional spring MVC program there will be two WebApplicationContext , one is the parent, from applicationContext.xml Riga, one is child, servlet-context.xml loaded from Riga.
The two are inheritance relationships, and child webapplicationcontext can getParent() get to root webapplicationcontext through functions.

Simply put, the bean in the child webapplicationcontext can be injected into the root webapplicationcontext bean, and the parent Webapplicationcontext beans cannot be injected into the bean in child webapplicationcontext.

A typical content of Web. XML is:

    <!--the definition of the Root Spring Container shared by all servlets and Filters-<Context-param><Param-name>contextconfiglocation</Param-name><Param-value>classpath*:/applicationcontext.xml</Param-value></Context-param><!--creates the Spring Container shared by all servlets and Filters--<Listener><Listener-class>org.springframework.web.context.contextloaderlistener</Listener-class></Listener><!--Processes Application requests--<Servlet><Servlet-name>appservlet</Servlet-name><Servlet-class>org.springframework.web.servlet.dispatcherservlet</Servlet-class><Init-param><Param-name>contextconfiglocation</Param-name><Param-value>/web-inf/servlet-context.xml</Param-value></init-param> <load-on-startup> 1</load-on-startup> <async-supported>true</async-supported> Span class= "Hljs-tag" ></servlet> < servlet-mapping> <servlet-name >appservlet</servlet-name> < url-pattern>/</url-pattern> Span class= "Hljs-tag" ></SERVLET-MAPPING>       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

Where Root webapplicationcontext is initialized by listener, the child webapplicationcontext is initialized through the servlet.

In the applicationContext.xml Component-scan class, which is usually only non-controller, such as:

 <context: Component-scan base-package= "io.github.test" > <context:exclude-filter expression=< Span class= "Hljs-value" > "Org.springframework.stereotype.Controller" type= "annotation"/> <context:exclude-filter type= "annotation" expression= "Org.springframework.web.bind.annotation.ControllerAdvice"/> </ CONTEXT:COMPONENT-SCAN>           
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

servlet-context.xmlusually only Component-scan controller classes are used, such as:

    <context:component-scan base-package=  "Io.github.test.web" use-default-filters=  "false" > <context:include-filter expression= "Org.springframework.stereotype.Controller" Span class= "Hljs-attribute" >type= "annotation"/> <context:include-filter type= " Annotation "expression=" Org.springframework.web.bind.annotation.ControllerAdvice "/> </ CONTEXT:COMPONENT-SCAN>           
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

If you do not component-scan separately, you may have a problem with bean duplication initialization.

This is what spring officially recommends at the start of the exercise.

Root/child Webapplicationcontext the problem of inheritance

The beans in root webapplicationcontext can be shared among different child webapplicationcontext, and the bean areas of the different child webapplicationcontext do not interfere, This was supposed to be a good design.

But there are actually a lot of problems:
* Many developers do not know that spring MVC is divided into two webapplicationcontext, resulting in a variety of repetitive construction beans, a variety of beans can not inject problems.
* There are some beans, such as the global AOP processing class, if root Webapplicationcontext is initialized, then the initialized bean in child webapplicationcontext is not processed. If initialized in child webapplicationcontext, there is no way to inject the class in Root webapplicationcontext.
* It is troublesome to distinguish which beans are placed in the Root/child, careless and error-prone.

Once and for all: beans are loaded by root Webapplicationcontext

When configuring metrics-spring at once, configure @EnableMetrics the configuration in which webapplicationcontext, feeling very sore. Finally, I decided to try to move all the beans, including the controller, to root webapplicationcontext, which applicationContext.xml is loaded in Riga, which servlet-context.xml is basically empty. The result is that the program runs completely fine.

After searching the internet, we found some related discussions:

Http://forum.spring.io/forum/spring-projects/container/89149-servlet-context-vs-application-context

The approach of Spring boot

The Component-scan configuration is not required by default in spring boot, so guess if there is only one webapplicationcontext in spring boot?

After the test, we found that in spring boot there is really only one webapplicationcontext: so it's a org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext lot easier in spring boot.

Summarize

Spring's ApplicationContext inheritance mechanism is a good design, and in many other places you can see similar ideas, such as Java's class loader. But in most spring Web applications, just one webapplicationcontext is actually enough. If separating Rott/child Webapplicationcontext can lead to confusion, and it doesn't work.

It is recommended that all Service/controller be moved to root webapplicationcontext initialization.

http://blog.csdn.net/hengyunabc/article/details/47072637

On the inheritance relationship of Spring MVC's Webapplicationcontext

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.