1. 程式人生 > >Spring 的 IOC 容器和 SpringMVC 的 IOC 容器 關係

Spring 的 IOC 容器和 SpringMVC 的 IOC 容器 關係

需要進行 Spring 整合 SpringMVC 嗎 ?
還是否需要再加入 Spring 的 IOC 容器 ?
是否需要再 web.xml 檔案中配置啟動 Spring IOC 容器的 ContextLoaderListener ?

1. 需要: 通常情況下, 類似於資料來源, 事務, 整合其他框架都是放在 Spring 的配置檔案中(而不是放在 SpringMVC 的配置檔案中).

實際上放入 Spring 配置檔案對應的 IOC 容器中的還有 Service 和 Dao. 

2. 不需要: 都放在 SpringMVC 的配置檔案中. 也可以分多個 Spring 的配置檔案, 然後使用 import 節點匯入其他的配置檔案

Spring 的 IOC 容器和 SpringMVC 的 IOC 容器掃描的包有重合的部分, 就會導致有的 bean 會被建立 2 次,如下

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	
	<!-- 配置啟動 Spring IOC 容器的 Listener -->
	<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
		
     <context:component-scan base-package="com.me"></context:component-scan>
	

	<!-- 配置資料來源, 整合其他框架, 事務等. -->

</beans>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<context:component-scan base-package="com.me"/>

	<!-- 配置檢視解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven></mvc:annotation-driven>
	
</beans>
上面的配置會導致 Bean 被建立兩次

所以 Spring 的 IOC 容器不應該掃描 SpringMVC 中的 bean, 對應的SpringMVC 的 IOC 容器不應該掃描 Spring 中的 bean


解決:

1. 使 Spring 的 IOC 容器掃描的包和 SpringMVC 的 IOC 容器掃描的包沒有重合的部分. 

但是有時候我們按業務模組建立包名,就不可避免有包重合,所以使用第二種方法


2. 使用 exclude-filter 和 include-filter 子節點來規定只能掃描的註解

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
		
     <context:component-scan base-package="com.me"><!--讓spring IOC 不掃描指定註解-->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>
	

	<!-- 配置資料來源, 整合其他框架, 事務等. -->

</beans>

springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	
	<!--use-default-filters="false"表示不要使用預設的過濾器,此處的預設過濾器,會掃描包含Service,Component,Repository,Controller註解修飾的類;  
        只讓springmvc IOC 掃描指定註解  
     -->  
	<context:component-scan base-package="com.atguigu.springmvc" use-default-filters="false">
		<context:include-filter type="annotation" 
			expression="org.springframework.stereotype.Controller"/>
		<context:include-filter type="annotation" 
			expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>

	<!-- 配置檢視解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven></mvc:annotation-driven>
	
</beans>

SpringMVC 的 IOC 容器是 Spring IOC 容器的子容器
SpringMVC 的 IOC 容器中的 bean 可以來引用 Spring IOC 容器中的 bean. 
返回來呢 ? 反之則不行. Spring IOC 容器中的 bean 卻不能來引用 SpringMVC IOC 容器中的 bean!
多個 Spring IOC 容器之間可以設定為父子關係,以實現良好的解耦。
Spring MVC WEB 層容器可作為 “業務層” Spring容器的子容器:即 WEB 層容器可以引用業務層容器的 Bean,而業務層容器卻訪問不到 WEB 層容器的 Bean
SpringMVC 的 IOC 容器是 Spring IOC 容器關係圖解