ApplicationListener事件之EmbeddedServletContainerInitializedEvent之介绍
来源:互联网 发布:曹查理 知乎 编辑:程序博客网 时间:2024/06/07 16:18
ApplicationListener事件
有时候需要在项目初始化的时候进行一系列工作,比如初始化一个线程池,初始化配置文件,初始化缓存等等,这时候就需要用到启动监听器,applicationListener在使用过程中可以监听某一事件的发生,可以做出相应的处理,这个方式不常用,但是在特殊情况下面还是有用的。
通过查看源代码ApplicationEvent事件
上面是Spring启动可能发布的一些事件,这里介绍我讲介绍两个事件,EmbeddedServletContainerInitializedEvent,这个事件可以用来获取到Servlet容器启动的端口
/** * 监听EmbeddedServletContainerInitializedEvent事件,用来获取Servlet容器初始化的端口 * * @author will * @author qianyi * @author garen * @since 1.0 */@Componentpublic class EmbeddedServletContainerInitializedEventListener implements ApplicationListener<EmbeddedServletContainerInitializedEvent> { @Override public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) { ServerPortHolder.setPort(event.getSource().getPort()); }}
第二个事件是图中的ApplicationReadyEvent,通常如果我们想在应用程序启动之后获取一些启动信息,可以监听这个事件,然后完成一系列的事件,比如获取启动的ContextPath
,这里因为项目中集成了Swagger接口文档,在应用程序启动完成之后,我们可以打印出Swagger文档的访问路径
/** * 应用程序启动监听,准备Swagger接口文档 * * @author will * @author qianyi * @author garen * @since 1.0 */@Componentpublic class ApplicationReadyListener implements ApplicationListener<ApplicationReadyEvent> { public static final Logger LOGGER = LoggerFactory.getLogger(ApplicationReadyListener.class); @Autowired private ServerProperties properties; @Override public void onApplicationEvent(ApplicationReadyEvent event) { LOGGER.debug("Application Ready"); String ctx = properties.getContextPath(); if (StringUtils.isEmpty(ctx)) { ctx = "/"; } else { ctx = ctx + "/"; } String portPart = ""; Integer port = ServerPortHolder.getPort(); if (port!=80) { portPart = ":" + port; } String host = null; try { host = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { // e.printStackTrace(); } if (StringUtils.isEmpty(host)) { host = "127.0.0.1"; } String message = String.format("Access URLS: External: %s%s%s%s%s", "http://", host, portPart, ctx, "swagger-ui.html"); message+= String.format(" , %s%s%s%s%s", "http://", host, portPart, ctx, ""); LOGGER.info(message); }}
这里我们可以看到,启动之后,我们的Console会打印出我们的访问地址:
阅读全文
0 0
- ApplicationListener事件之EmbeddedServletContainerInitializedEvent之介绍
- libgdx游戏引擎(二)之ApplicationListener接口介绍
- Spring中使用ApplicationContext进行事件的监听之ApplicationEvent,ApplicationListener
- Spring之—ApplicationListener 接口及事务管理
- 【Spring】事件传播 ApplicationListener
- exths4之tree事件加载介绍
- Android之---事件分发机制详细介绍
- Spring ApplicationContext事件机制:ApplicationEvent与ApplicationListener
- Spring 通过ApplicationListener&ApplicationEvent实现事件监听
- Spring透过ApplicationListener来触发contextrefreshedevent事件
- js之事件冒泡和事件捕获详细介绍
- js之事件冒泡和事件捕获详细介绍
- js之事件冒泡和事件捕获详细介绍
- js之事件冒泡和事件捕获详细介绍
- js之事件冒泡和事件捕获详细介绍
- js之事件冒泡和事件捕获详细介绍
- js之事件冒泡和事件捕获详细介绍
- js之事件冒泡和事件捕获详细介绍
- super.getClass()与this.getClass() 的区别
- git 常用命令
- WebView
- 微信小程序实例源码大全
- 排队、接续座席
- ApplicationListener事件之EmbeddedServletContainerInitializedEvent之介绍
- CentOS 安装 Apache PHP MYSQL Memcached Memcache 环境配置
- 基于Keystone架构的DSP
- 深入理解string和如何高效地使用string
- poi(Excal)常用属性(自用)
- 拟合回归方法小结
- Java HashMap笔记之三:ConcurrentHashMap线程安全原理
- 数据库SQL实战
- 转接坐席标签