一、Spring和SpringMVC的父子容器關係
1.講問題之前要先明白一個關係
一般來說,我們在整合Spring和SpringMVC這兩個框架中,web.xml會這樣寫到:
<!-- 加載spring容器 -->
<!-- 初始化加載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>
<!-- 配置springmvc前端控制器 -->
<servlet>
<servlet-name>taotao-manager</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation,
springmvc的配置文件默認在: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>
首先配置的是Spring容器的初始化加載的application文件,然後是SpringMVC的前端控制器(DispatchServlet),當配置完DispatchServlet後會在Spring容器中創建一個新的容器。其實這是兩個容器,Spring作爲父容器,SpringMVC作爲子容器。
讓我們用圖來看一下這個父子關係的原理
平時我們在項目中注入關係是這樣的順序(結合圖來說):在Service中注入Dao(初始化自動注入,利用@Autowired),接着在Controller裏注入Service(初始化自動注入,利用@Autowired),看圖,這就意味這作爲SpringMVC的子容器是可以訪問父容器Spring對象的。
那麼問大家一個問題。要是反過來呢,你把Controller注入到Service中能行麼?
肯定是不行的啊!(如圖,這也說明了父容器是不能調用子容器對象的)
如果Dao,Serive,Controller要是都在Spring容器中,無疑上邊的問題是肯定的,因爲都是在一個bean裏,一個容器中。
2.問題:爲什麼不能在Spring中的Service層配置全局掃描?
例如:一個項目中我總項目的名字叫com.shop,我們在配置applicationContext-service.xml中,包掃描代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
...../ 此處省略>
<!-- 掃描包Service實現類 -->
<context:component-scan base-package="com.shop.service"></context:component-scan>
</beans>
上面所配置的是一個局部掃描,而不是全局掃描。接下來說原因:
這裏就和上面講到的父子容器有關係,假設我們做了全局掃描那麼代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
...../ 此處省略>
<!-- 掃描包Service實現類 -->
<context:component-scan base-package="com.shop"></context:component-scan>
</beans>
此時的Spring容器中就會掃描到@Controller,@Service,@Reposity,@Component,此時的圖如下
結合圖去看,相當於他們都會放到大的容器中,而這時的SpringMVC容器中沒有對象,沒有對象就沒有Controller,所以加載處理器,適配器的時候就會找不到映射對象,映射關係,因此在頁面上就會出現404的錯誤。
3.如果不用Spring容器,直接把所有層放入SpringMVC容器中可不可以?
當然可以,如果沒有Spring容器,我們是可以把所有層放入SpringMVC的。單獨使用這個容器是完全可以的,而且是輕量級的。
4.那麼爲什麼我們在項目中還要聯合用到Spring容器和SpringMVC容器?
答案是: Spring的擴展性,如果要是項目需要加入Struts等可以整合進來,便於擴展框架。如果要是爲了快,爲了方便開發,完全可以用SpringMVC框架。
5.結論
如果在項目中我們在Service層做全局包掃描,那麼springmvc不能提供服務,因爲springmvc子容器中沒有controller對象。