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容器对象,可以看到它有值了

更多请见:http://www.mark-to-win.com/tutorial/51185.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值