SpringBoot扩展点之三:SpringBootServletInitializer扩展
SpringBootServletInitializer
熟悉了SpringApplication的原理之后,我们再来了解SpringBootServletInitializer的原理就比较容易了。
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoWarApplication.class);
}
}
SpringBootServletInitializer就是一个org.springframework.web.context.WebApplicationContext,容器启动时会调用其onStartup(ServletContext servletContext)方法,接下来我么就来看一下这个方法:
public void onStartup(ServletContext servletContext) throws ServletException {
this.logger = LogFactory.getLog(this.getClass());
final WebApplicationContext rootAppContext = this.createRootApplicationContext(servletContext);
if(rootAppContext != null) {
servletContext.addListener(new ContextLoaderListener(rootAppContext) {
public void contextInitialized(ServletContextEvent event) {
}
});
} else {
this.logger.debug("No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context");
}
}
这里的核心方法就是createRootApplicationContext(servletContext):
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
//创建SpringApplicationBuilder,并用其生产出SpringApplication对象
SpringApplicationBuilder builder = this.createSpringApplicationBuilder();
builder.main(this.getClass());
ApplicationContext parent = this.getExistingRootWebApplicationContext(servletContext);
if(parent != null) {
this.logger.info("Root context already created (using as parent).");
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, (Object)null);
builder.initializers(new ApplicationContextInitializer[]{new ParentContextApplicationContextInitializer(parent)});
}
//初始化并封装SpringApplicationBuilder对象,为SpringApplication对象增加ApplicationContextInitializer和ApplicationListener做准备
builder.initializers(new ApplicationContextInitializer[]{new ServletContextApplicationContextInitializer(servletContext)});
builder.listeners(new ApplicationListener[]{new ServletContextApplicationListener(servletContext)});
//指定创建的ApplicationContext类型
builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
//传递入口类,并构建SpringApplication对象
//可以通过configure()方法对SpringBootServletInitializer进行扩展
builder = this.configure(builder);
SpringApplication application = builder.build();
if(application.getSources().isEmpty() && AnnotationUtils.findAnnotation(this.getClass(), Configuration.class) != null) {
application.getSources().add(this.getClass());
}
Assert.state(!application.getSources().isEmpty(), "No SpringApplication sources have been defined. Either override the configure method or add an @Configuration annotation");
if(this.registerErrorPageFilter) {
application.getSources().add(ErrorPageFilter.class);
}
//最后调用SpringApplication的run方法
return this.run(application);
}
说明
SpringBootServletInitializer的执行过程,简单来说就是通过SpringApplicationBuilder构建并封装SpringApplication对象,并最终调用SpringApplication的run方法的过程。
扩展SpringBootServletInitializer
与扩展SpringApplication类似,ApplicationContextInitializer和ApplicationListener可以基于SpringApplicationBuilder提供的public方法进行扩展
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
application.initializers(MyApplicationContextInitializer1,MyApplicationContextInitializer2);
application.listeners(MyApplicationListener1,MyApplicationListener2)
return application.sources(DemoWarApplication.class);
}
}
SpringBoot扩展点之三:SpringBootServletInitializer扩展的更多相关文章
- Chrome扩展开发之三——Chrome扩展中的数据本地存储和下载
目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...
- Chrome扩展开发之一——Chrome扩展的文件结构
目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...
- spring-boot的spring-cache中的扩展redis缓存的ttl和key名
原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...
- spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情
<spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情> <服务网关zu ...
- Asp.net 面向接口可扩展框架之“Mvc扩展框架及DI”
标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把以前的那个Mvc分区扩展框架迁移过来,并优化整 ...
- win10 64位安装memcache扩展和开启redis扩展
前面有关于win10下搭建wamp环境的介绍,在此不在赘述,php操作memcache有memcache库和memcached库,其中memcache是php内置的扩展库,支持面向对象和面向过程两种操 ...
- PHP5不重新编译,如何安装自带的未安装过的扩展,如soap扩展?
在虚拟机的CentOS5.5中,一键安装了PHP运行环境,但发现并没有 soap 扩展,而近期项目用需要用到 webservice. 上述的一键安装(lamp0.4),其实是源码编译安装,PHP配置文 ...
- PHP扩展编写、PHP扩展调试、VLD源码分析、基于嵌入式Embed SAPI实现opcode查看
catalogue . 编译PHP源码 . 扩展结构.优缺点 . 使用PHP原生扩展框架wizard ext_skel编写扩展 . 编译安装VLD . Debug调试VLD . VLD源码分析 . 嵌 ...
- 【小结】有关mysql扩展库和mysqli扩展库的crud操作封装
现阶段php如果要操作mysql数据库 php给我们提供了3套库 1.mysql扩展库 面向过程操作 2.mysqli扩展库 面向对象操作和面向过程操作并存 安全性和效率高于mysql扩展库 ...
- 面向接口可扩展框架之“Mvc扩展框架及DI”
面向接口可扩展框架之“Mvc扩展框架及DI” 标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把 ...
随机推荐
- 使用Dictionary键值对判断字符串中字符出现次数
介绍Dictionary 使用前需引入命名空间 using System.Collections.Generic Dictionary里面每一个元素都是一个键值对(由两个元素组成:键和值) 键必须是唯 ...
- iOS7的十个更“佳”:简洁直观更受青睐
转自:http://www.25pp.com/news/news_27792.html iOS7自发布以来一直是褒贬不一,虽然苹果还只是发布了第二个测试版,但普通用户早已经在纠结到底该不该升级iOS7 ...
- 实验吧——因缺思汀的绕过(sql with rollup)
题目地址:http://ctf5.shiyanbar.com/web/pcat/index.php 通读源码,得知出flag的条件 1.需要post提交uname以及pwd,否则直接die了 if ( ...
- Vue入门篇
Vue-cli开发环境搭建 1. 安装nodejs 2. 设置缓存文件夹 $ npm config set cache "D:\vueProject\nodejs\node_cache&qu ...
- 服务刚启动就 Old GC,要闹哪样?
1.背景 最近有个同学说他的服务刚启动就收到两次 Full GC 告警, 按道理来说刚启动,对象应该不会太多,为啥会触发 Full GC 呢? 带着疑问,我们还是先看看日志吧,毕竟日志的信息更多. 2 ...
- Spark GraphX图计算核心源码分析【图构建器、顶点、边】
一.图构建器 GraphX提供了几种从RDD或磁盘上的顶点和边的集合构建图形的方法.默认情况下,没有图构建器会重新划分图的边:相反,边保留在默认分区中.Graph.groupEdges要求对图进行重新 ...
- mac运行模拟器simulator突然很慢
一直都正常,突然变慢,而且慢的离谱. 上网查了下,这里记录下,或许问题不仅限于此. simulator->Debug->Slow Animations. 这个Slow Animations ...
- 2. 代理模式(C++)
1.介绍 代理模式:为其他对象提供一种代理以控制对这个对象的访问.这样实现了业务和核心功能分离. 在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口.在某些情况下,一个对象不适合或者不能 ...
- Makefile 中各种函数列举(进行 ing)
1.wildcard : 扩展通配符 notdir : 去除路径 patsubst :替换通配符 举例:有文件 /a.c /b.c /home/a.c /home/b.c ...
- node 单线程异步非阻塞
链接:http://www.runoob.com/nodejs/nodejs-callback.html 首先什么是单线程异步非阻塞? 单线程的意思整个程序从头到尾但是运用一个线程,程序是从上往下执行 ...