Spring容器启动过程

搞了一年多的Java了,每个项目都在用Spring,这几天没事看了看Spring源码,总结了下Spring容器的启动过程,想把它记录下来,免得忘了

spring容器的启动方式有两种:

1、自己提供ApplicationContext自己创建Spring容器

2、Web项目中在web.xml中配置监听启动

org.springframework.web.context.ContextLoaderListener

先介绍第一种(自创建)

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml")
User user = (User) context.getBean()}

当通过ClassPathApplicationContext初始化容器时,它会根据定位加载spring.xml配置,然后解析配置文件,生成Bean,注册Bean,最后我们在通过getBean获取对象,这一现象就跟IOC容器的初始化过程一样,资源定位、资源加载、资源解析、生成Bean、Bean注册

我们再来说说第二种初始化方式:

第二种在web.xml文件中进行配置,根据web容器的启动而启动

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

ContextLoaderListener实现了ServletContextListener接口,实现了两个方法

//初始化WebApplicationContext容器
public void contextInitialized(ServletContextEvent event) {
    this.initWebApplicationContext(event.getServletContext());
}

//销毁WebApplicationContext容器
public void contextDestroyed(ServletContextEvent event) {
    this.closeWebApplicationContext(event.getServletContext());
    ContextCleanupListener.cleanupAttributes(event.getServletContext());
}

参数ServletContextEvent能直接获取servletContext也就是java web容器的上下文

在父类中调用了intitWebApplicationContext方法,传入了ServletContext,在父类方法中对ServletContext进行了判断,检测servletContext的属性中是否存在spring的根上下文属性,如果存在则是错误的,因为表明已经有一个根上下文已经启动,再启动会冲突,所以避免重复启动!

(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != ) {
    IllegalStateException()}
时间: 2024-07-29 08:06:21

Spring容器启动过程的相关文章

spring的启动过程就是创建ioc容器的过程

1. spring简介 spring的最基本的功能就是创建对象及管理这些对象之间的依赖关系,实现低耦合.高内聚.还提供像通用日志记录.性能统计.安全控制.异常处理等面向切面的能力,还能帮我们管理最头疼的数据库事务,本身提供了一套简单的JDBC访问实现,提供与 第三方数据访问框架集成(如hibernate.JPA),与各种Java EE技术整合(如JavaMail.任务调度等等),提供一套自己的web层框架Spring MVC.而且还能非常简单的与第三方web框架集成.从这里我们可以认为Sprin

Spring 源码学习(二) IOC容器启动过程

这一节主要是记录一下Spring Ioc 容器的启动过程. Spring 的 Ioc 容器是怎么被加载和使用的? web容器为它提供了宿主环境 ServlectContext,  Tomcat 启动时会读取web.xml. 并且实例化web.xml中配置的ContextLoaderListener ,下面看一下ContextLoaderListener的创建过程: 在实例化ContextLoaderListener 之后,通过接口回调执行ContextLoaderListener 类中的cont

Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动

之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void createEmbeddedServletContainer() { EmbeddedServletContainer localContainer = this.embeddedServletContainer; ServletContext localServletContext = getServ

spring容器启动之我见

spring容器启动之我见. 本人也是自己看看源码,然后方便以后自己记忆和理解,故写此文章,如果有什么错的地方还请大家提出. 针对于tomcat做服务器的项目,我们首先看的就是web.xml文件,spring容器启动去加载监听器 看如下代码:其监听器使用spring api中的类ContextLoaderListener <context-param> <param-name>contextConfigLocation</param-name> <param-va

Spring容器启动后注入service到Servlet并自动执行

通常做法是定义一个Servlet,并在web.xml中配置Servlet的启动顺序<load-on-startup>的值在DispatcherServlet之后.但这样做的缺点是在Servlet中无法使用Spring的依赖注入功能,只能使用WebApplicationContext的getBean()方法获取bean. 找到的解决办法如下: 1.自定义一个用于代理启动Servlet的类DelegatingServletProxy: package cn.edu.swu.oa.common.ut

Spring Boot启动过程(三)

我已经很精简了,两篇(Spring Boot启动过程(一).pring Boot启动过程(二))依然没写完,接着来. refreshContext之后的方法是afterRefresh,这名字起的真...好.afterRefresh方法内只调用了callRunners一个方法,这个方法从上下文中获取了所有的ApplicationRunner和CommandLineRunner接口的实现类,并执行这些实现类的run方法.例如Spring Batch的JobLauncherCommandLineRun

Spring Boot启动过程及回调接口汇总

Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qian) 注:本文基于spring-boot 1.4.1.RELEASE, spring 4.3.3.RELEASE撰写. 启动顺序 Spring boot的启动代码一般是这样的: 1 2 3 4 5 6 @SpringBootApplication public class SampleApplica

Fabric1.4源码中链码容器启动过程解析

想写点东西记录一下最近看的一些Fabric源码,本文使用的是fabric1.4的版本,所以对于其他版本的fabric,内容可能会有所不同. 其实我仅仅知道Go语言一些语法的使用,并不太熟悉Go语言,所以解析的内容可能会有误,欢迎大家批评指正. 本文想针对Fabric中链码容器的启动过程进行源码的解析.这里的链码指的是用户链码不是系统链码,顺便回顾一下系统链码: lscc(Life Cycle System ChainCode)生命周期系统链码 cscc(Configuration System

spring容器启动的加载过程

使用spring,我们在web.xml都会配置ContextLoaderListener <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> public class ContextLoaderListener extends ContextLoader implements Servle