zoukankan      html  css  js  c++  java
  • Spring框架:第九章:Spring整合Web

    Spring整合Web
    在web工程中添加Spring的jar包。
    Spring的核心包
    spring-beans-4.0.0.RELEASE.jar
    spring-context-4.0.0.RELEASE.jar
    spring-core-4.0.0.RELEASE.jar
    spring-expression-4.0.0.RELEASE.jar
    aop包
    spring-aop-4.0.0.RELEASE.jar
    spring-aspects-4.0.0.RELEASE.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    JDBC-ORM包
    spring-jdbc-4.0.0.RELEASE.jar
    spring-orm-4.0.0.RELEASE.jar
    spring-tx-4.0.0.RELEASE.jar
    Spring的web整合包
    spring-web-4.0.0.RELEASE.jar
    测试包
    spring-test-4.0.0.RELEASE.jar

    整合Spring和Web容器分两个步骤:
    1、导入spring-web-4.0.0.RELEASE.jar
    2、在web.xml配置文件中配置org.springframework.web.context.ContextLoaderListener监听器监听ServletContext的初始化
    3、在web.xml配置文件中配置contextConfigLocation上下文参数。配置Spring配置文件的位置,以用于初始化Spring容器

    在web.xml中配置

      <!-- needed for ContextLoaderListener -->
      <!--配置SpringIOC容器的配置文件路径-->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>location</param-value>
    	</context-param>
    
    	<!-- Bootstraps the root web application context before servlet initialization -->
    	<!--这个监听器会在web工程启动时候创建Spring IOC 容器。-->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    

    这个监听器会在web工程启动时候读取spring的配置文件,做初始化Spring IOC 容器的操作。
    这个Spring IOC 容器对象保存到ServletContext域对象中我们就可以使用Spring IOC容器对象。
    我们可以深入进去看看底层源码:
    public class ContextLoaderListener extends ContextLoader implements ServletContextListener
    可以看到它实现了ServletContextListener,ServletContextListener监听servletContext对象的创建和销毁
    在ServletContextListener里可以看到有contextInitialized和contextDestroyed

      // Method descriptor #5 (Ljavax/servlet/ServletContextEvent;)V
      public abstract void contextInitialized(javax.servlet.ServletContextEvent arg0);
      
      // Method descriptor #5 (Ljavax/servlet/ServletContextEvent;)V
      public abstract void contextDestroyed(javax.servlet.ServletContextEvent arg0);
    

    contextInitialized是servletContext对象的创建
    contextDestroyed是servletContext对象的销毁

    servletContext对象的创建在web工程启动的时候创建,web工程启动的时候需要有一个springIOC容器
    可以看看我们之前的代码@ContextConfiguration(locations = “classpath:applicationContext.xml”)或者
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);都创建了Spring的IOC容器对象

    在创建了Spring的IOC容器对象时我们需要Spring的配置文件:applicationContext.xml
    我们在web.xml中配置

      <!--配置SpringIOC容器的配置文件路径-->
        	<context-param>
        		<param-name>contextConfigLocation</param-name>
        		<param-value>classpath:applicationContext.xml</param-value>
        	</context-param>
    

    下面这个监听器一启动就会读取上面的参数:配置文件路径

    <!--这个监听器会在web工程启动时候创建Spring IOC 容器。-->
        	<listener>
        		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        	</listener>
    

    我们可以使用DeBug看源码ContextLoaderListener的initWebApplicationContext(event.getServletContext());找到ContextLoader中initWebApplicationContext方法
    createWebApplicationContext创建一个Spring容器对象,可以看到它有值了
    在这里插入图片描述

    可以继续往下看configureAndRefreshWebApplicationContext配置和刷新Spring容器
    我们进入它的方法体内找到以下代码
    String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
    在这里插入图片描述

    它来读取web.xml中<param-name>contextConfigLocation</param-name>的值,获得初始化参数,得到我们自己写的<param-value>classpath:applicationContext.xml</param-value>
    在这里插入图片描述

    把它放到Spring容器中

    在这里插入图片描述

    执行到wac.refresh();位置就开始刷新,执行后要等上一段时间,执行完这段代码后,我们的配置文件applicationContext.xml已经全部加载好了,bean对象也创建好了。

    回到initWebApplicationContext方法中执行到
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

    其中this.context
    在这里插入图片描述
    它把容器对象放到了servletContext域对象中,使用在web工程中只要获取到servletContext对象就可以在servletContext对象中获取spring容器对象

    这就是在web.xml中配置的底层原理

    我们如何获取到spring容器对象也就是WebApplicationContext呢?

    获取WebApplicationContext上下文对象的方法如下:
    方法一(不推荐):
    getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    这一个好理解,前面我们容器对象放到了servletContext域对象中,现在获取就好了,不过后面的一长串不好记忆,所以不推荐

    方法二(推荐):
    WebApplicationContextUtils.getWebApplicationContext(getServletContext())
    使用这种的就好些了,不用我们记忆,而且它的底层和方法一是一样的,也是获取getAttribute

  • 相关阅读:
    PythonのTkinter基本原理
    使用 Word (VBA) 分割长图到多页
    如何使用 Shebang Line (Python 虚拟环境)
    将常用的 VBScript 脚本放到任务栏 (Pin VBScript to Taskbar)
    关于 VBScript 中的 CreateObject
    Windows Scripting Host (WSH) 是什么?
    Component Object Model (COM) 是什么?
    IOS 打开中文 html 文件,显示乱码的问题
    科技发展时间线(Technology Timeline)
    列置换密码
  • 原文地址:https://www.cnblogs.com/javawxid/p/15644755.html
Copyright © 2011-2022 走看看