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中配置
<param-n
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
ame>contextConfigLocation
location
org.springframework.web.context.ContextLoaderListener
这个监听器会在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中配置
contextConfigLocation
classpath:applicationContext.xml
下面这个监听器一启动就会读取上面的参数:配置文件路径
org.springframework.web.context.ContextLoaderListener
我们可以使用DeBug看源码ContextLoaderListener的initWebApplicationContext(event.getServletContext());找到ContextLoader中initWebApplicationContext方法