一、背景
最近由于項目的包掃描出現了問題,在解決問題的過程中,偶然發現了Spring和SpringMVC是有父子容器關系的,而且正是因為這個才往往會出現包掃描的問題,我們在此來分析和理解Spring和SpringMVC的父子容器關系并且給出Spring和SpringMVC配置文件中包掃描的官方推薦方式。
二、概念理解和知識鋪墊
在Spring整體框架的核心概念中,容器是核心思想,就是用來管理Bean的整個生命周期的,而在一個項目中,容器不一定只有一個,Spring中可以包括多個容器,而且容器有上下層關系,目前最常見的一種場景就是在一個項目中引入Spring和SpringMVC這兩個框架,那么它其實就是兩個容器,Spring是父容器,SpringMVC是其子容器,并且在Spring父容器中注冊的Bean對于SpringMVC容器中是可見的,而在SpringMVC容器中注冊的Bean對于Spring父容器中是不可見的,也就是子容器可以看見父容器中的注冊的Bean,反之就不行。
我們可以使用統一的如下注解配置來對Bean進行批量注冊,而不需要再給每個Bean單獨使用xml的方式進行配置。
1
|
< context:component-scan base-package = "com.hafiz.www" /> |
從Spring提供的參考手冊中我們得知該配置的功能是掃描配置的base-package包下的所有使用了@Component注解的類,并且將它們自動注冊到容器中,同時也掃描@Controller,@Service,@Respository這三個注解,因為他們是繼承自@Component。
在項目中我們經常見到還有如下這個配置,其實有了上面的配置,這個是可以省略掉的,因為上面的配置會默認打開以下配置。以下配置會默認聲明了@Required、@Autowired、 @PostConstruct、@PersistenceContext、@Resource、@PreDestroy等注解。
1
|
< context:annotation-config /> |
另外,還有一個和SpringMVC相關如下配置,經過驗證,這個是SpringMVC必須要配置的,因為它聲明了@RequestMapping、@RequestBody、@ResponseBody等。并且,該配置默認加載很多的參數綁定方法,比如json轉換解析器等。
1
|
< mvc:annotation-driven /> |
而上面這句配置spring3.1之前的版本和以下配置方式等價
1
2
3
4
|
<!--配置注解控制器映射器,它是SpringMVC中用來將Request請求URL到映射到具體Controller--> < bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <!--配置注解控制器映射器,它是SpringMVC中用來將具體請求映射到具體方法--> < bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> |
spring3.1之后的版本和以下配置方式等價
1
2
3
4
|
<!--配置注解控制器映射器,它是SpringMVC中用來將Request請求URL到映射到具體Controller--> < bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <!--配置注解控制器映射器,它是SpringMVC中用來將具體請求映射到具體方法--> < bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> |
三、具體場景分析
下面讓我們來詳細扒一扒Spring與SpringMVC的容器沖突的原因到底在那里?
我們共有Spring和SpringMVC兩個容器,它們的配置文件分別為applicationContext.xml和applicationContext-MVC.xml。
1.在applicationContext.xml中配置了<context:component-scan base-package=“com.hafiz.www" />,負責所有需要注冊的Bean的掃描和注冊工作。
2.在applicationContext-MVC.xml中配置<mvc:annotation-driven />,負責SpringMVC相關注解的使用。
3.啟動項目我們發現SpringMVC無法進行跳轉,將log的日志打印級別設置為DEBUG進行調試,發現SpringMVC容器中的請求好像沒有映射到具體controller中。
4.在applicationContext-MVC.xml中配置<context:component-scan base-package=“com.hafiz.www" />,重啟后,驗證成功,springMVC跳轉有效。
下面我們來查看具體原因,翻看源碼,從SpringMVC的DispatcherServlet開始往下找,我們發現SpringMVC初始化時,會尋找SpringMVC容器中的所有使用了@Controller注解的Bean,來確定其是否是一個handler。1,2兩步的配置使得當前springMVC容器中并沒有注冊帶有@Controller注解的Bean,而是把所有帶有@Controller注解的Bean都注冊在Spring這個父容器中了,所以springMVC找不到處理器,不能進行跳轉。核心源碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
protected void initHandlerMethods() { if (logger.isDebugEnabled()) { logger.debug( "Looking for request mappings in application context: " + getApplicationContext()); } String[] beanNames = ( this .detectHandlerMethodsInAncestorContexts ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object. class ) : getApplicationContext().getBeanNamesForType(Object. class )); for (String beanName : beanNames) { if (isHandler(getApplicationContext().getType(beanName))){ detectHandlerMethods(beanName); } } handlerMethodsInitialized(getHandlerMethods()); } |
在方法isHandler中會判斷當前bean的注解是否是controller,源碼如下:
1
2
3
|
protected boolean isHandler(Class<?> beanType) { return AnnotationUtils.findAnnotation(beanType, Controller. class ) != null ; } |
而在第4步配置中,SpringMVC容器中也注冊了所有帶有@Controller注解的Bean,故SpringMVC能找到處理器進行處理,從而正常跳轉。
我們找到了出現不能正確跳轉的原因,那么它的解決辦法是什么呢?
我們注意到在initHandlerMethods()方法中,detectHandlerMethodsInAncestorContexts這個Switch,它主要控制獲取哪些容器中的bean以及是否包括父容器,默認是不包括的。所以解決辦法就是在springMVC的配置文件中配置HandlerMapping的detectHandlerMethodsInAncestorContexts屬性為true即可(這里需要根據具體項目看使用的是哪種HandlerMapping),讓它檢測父容器的bean。如下:
1
2
3
4
5
|
< bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" > < property name = "detectHandlerMethodsInAncestorContexts" > < value >true</ value > </ property > </ bean > |
但在實際工程中會包括很多配置,我們按照官方推薦根據不同的業務模塊來劃分不同容器中注冊不同類型的Bean:Spring父容器負責所有其他非@Controller注解的Bean的注冊,而SpringMVC只負責@Controller注解的Bean的注冊,使得他們各負其責、明確邊界。配置方式如下
1.在applicationContext.xml中配置:
1
2
3
4
|
<!-- Spring容器中注冊非@controller注解的Bean --> < context:component-scan base-package = "com.hafiz.www" > < context:exclude-filter type = "annotation" expression = "org.springframework.stereotype.Controller" /> </ context:component-scan > |
2.applicationContext-MVC.xml中配置
1
2
3
4
|
<!-- SpringMVC容器中只注冊帶有@controller注解的Bean --> < context:component-scan base-package = "com.hafiz.www" use-default-filters = "false" > < context:include-filter type = "annotation" expression = "org.springframework.stereotype.Controller" /> </ context:component-scan > |
三、總結
這樣我們在清楚了spring和springMVC的父子容器關系、以及掃描注冊的原理以后,根據官方建議我們就可以很好把不同類型的Bean分配到不同的容器中進行管理。再出現Bean找不到或者SpringMVC不能跳轉以及事務的配置失效的問題,我們就可以很快的定位以及解決問題了。很開心,有木有~
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/hafiz/p/5875740.html