標籤:
spring mvc裡的root/child WebApplicationContext的繼承關係
在傳統的spring mvc程式裡會有兩個WebApplicationContext,一個是parent,從applicationContext.xml裡載入的,一個是child,從servlet-context.xml裡載入的。
兩者是繼承關係,child WebApplicationContext 可以通過getParent()函數擷取到root WebApplicationContext。
簡單地說child WebApplicationContext裡的bean可以注入root WebApplicationContext裡的bean,而parent WebApplicationContext的bean則不能注入child WebApplicationContext裡的bean。
一個典型的web.xml的內容是:
<!-- 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> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 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
- 21
- 22
- 23
- 24
- 25
- 26
- 27
其中root WebApplicationContext是通過listener初始化的,child WebApplicationContext是通過servlet初始化的。
而在applicationContext.xml裡通常只component-scan非Controller的類,如:
<context:component-scan base-package="io.github.test"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>
在servlet-context.xml裡通常只component-scan Controller類,如:
<context:component-scan base-package="io.github.test.web" use-default-filters="false"> <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>
如果不這樣子分別component-scan的話,可能會出現Bean重複初始化的問題。
上面是Spring官方開始時推薦的做法。
root/child WebApplicationContext繼承關係帶來的麻煩
root WebApplicationContext裡的bean可以在不同的child WebApplicationContext裡共用,而不同的child WebApplicationContext裡的bean區不干擾,這個本來是個很好的設計。
但是實際上有會不少的問題:
* 不少開發人員不知道Spring mvc裡分有兩個WebApplicationContext,導致各種重複構造bean,各種bean無法注入的問題。
* 有一些bean,比如全域的aop處理的類,如果先root WebApplicationContext裡初始化了,那麼child WebApplicationContext裡的初始化的bean就沒有處理到。如果在child WebApplicationContext裡初始化,在root WebApplicationContext裡的類就沒有辦法注入了。
* 區分哪些bean放在root/child很麻煩,不小心容易搞錯,而且費心思。
一勞永逸的解決辦法:bean都由root WebApplicationContext載入
在一次配置metrics-spring時,對配置@EnableMetrics配置在哪個WebApplicationContext裡,感到很蛋疼。最終決定試下把所有的bean,包括Controller都移到root WebApplicationContext,即applicationContext.xml裡載入,而servlet-context.xml裡基本是空的。結果發現程式運行完全沒問題。
後面在網上搜尋了下,發現有一些相關的討論:
http://forum.spring.io/forum/spring-projects/container/89149-servlet-context-vs-application-context
spring boot裡的做法
在spring boot裡預設情況下不需要component-scan的配置,於是猜測在Spring boot裡是不是只有一個WebApplicationContext?
後面測試下了,發現在spring boot裡預設情況下的確是只有一個WebApplicationContext:org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext,所以在spring boot裡省事了很多。
總結
spring 的ApplicationContext繼承機制是一個很好的設計,在很多其它地方都可以看到類似的思路,比如Java的class loader。但是在大部分spring web程式裡,實際上只要一個WebApplicationContext就夠了。如果分開rott/child WebApplicationContext會導致混亂,而沒什麼用。
所以推薦把所有的Service/Controller都移到root WebApplicationContext中初始化。
http://blog.csdn.net/hengyunabc/article/details/47072637
扯談spring mvc之WebApplicationContext的繼承關係