Spring 的 IOC 容器和 SpringMVC 的 IOC 容器 关系

来源:互联网 发布:宜家值得购买知乎 编辑:程序博客网 时间:2024/04/29 03:30
需要进行 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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.       
  7.     <!-- 配置启动 Spring IOC 容器的 Listener -->  
  8.     <!-- needed for ContextLoaderListener -->  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>classpath:beans.xml</param-value>  
  12.     </context-param>  
  13.   
  14.     <!-- Bootstraps the root web application context before servlet initialization -->  
  15.     <listener>  
  16.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  17.     </listener>  
  18.       
  19.     <servlet>  
  20.         <servlet-name>springDispatcherServlet</servlet-name>  
  21.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  22.         <init-param>  
  23.             <param-name>contextConfigLocation</param-name>  
  24.             <param-value>classpath:springmvc.xml</param-value>  
  25.         </init-param>  
  26.         <load-on-startup>1</load-on-startup>  
  27.     </servlet>  
  28.   
  29.     <servlet-mapping>  
  30.         <servlet-name>springDispatcherServlet</servlet-name>  
  31.         <url-pattern>/</url-pattern>  
  32.     </servlet-mapping>  
  33.       
  34. </web-app>  
bean.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
  7.           
  8.      <context:component-scan base-package="com.me"></context:component-scan>  
  9.       
  10.   
  11.     <!-- 配置数据源, 整合其他框架, 事务等. -->  
  12.   
  13. </beans>  
springmvc.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  8.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
  9.   
  10.     <context:component-scan base-package="com.me"/>  
  11.   
  12.     <!-- 配置视图解析器 -->  
  13.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  14.         <property name="prefix" value="/WEB-INF/views/"></property>  
  15.         <property name="suffix" value=".jsp"></property>  
  16.     </bean>  
  17.     <mvc:default-servlet-handler/>  
  18.     <mvc:annotation-driven></mvc:annotation-driven>  
  19.       
  20. </beans>  


上面的配置会导致 Bean 被创建两次

所以 Spring 的 IOC 容器不应该扫描 SpringMVC 中的 bean, 对应的SpringMVC 的 IOC 容器不应该扫描 Spring 中的 bean


解决:

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

但是有时候我们按业务模块创建包名,就不可避免有包重合,所以使用第二种方法


2. 使用 exclude-filter 和 include-filter 子节点来规定只能扫描的注解

bean.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
  7.           
  8.      <context:component-scan base-package="com.me"><!--让spring IOC 不扫描指定注解-->  
  9.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  10.         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  11.     </context:component-scan>  
  12.       
  13.   
  14.     <!-- 配置数据源, 整合其他框架, 事务等. -->  
  15.   
  16. </beans>  

springmvc.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  8.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
  9.   
  10.       
  11.     <!--use-default-filters="false"表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含Service,Component,Repository,Controller注解修饰的类;    
  12.         只让springmvc IOC 扫描指定注解    
  13.      -->    
  14.     <context:component-scan base-package="com.atguigu.springmvc" use-default-filters="false">  
  15.         <context:include-filter type="annotation"   
  16.             expression="org.springframework.stereotype.Controller"/>  
  17.         <context:include-filter type="annotation"   
  18.             expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  19.     </context:component-scan>  
  20.   
  21.     <!-- 配置视图解析器 -->  
  22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  23.         <property name="prefix" value="/WEB-INF/views/"></property>  
  24.         <property name="suffix" value=".jsp"></property>  
  25.     </bean>  
  26.       
  27.     <mvc:default-servlet-handler/>  
  28.     <mvc:annotation-driven></mvc:annotation-driven>  
  29.       
  30. </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 容器关系图解

0 0