在阅读Springboot启动源码的时候,发现Springboot自动启动listeners是通过uopeizhi文件配置的,本文就是采用Springboot方式自动装入listeners。

项目依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>boot</finalName>
</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
项目监听配置

在resource目录下建META-INF文件夹,并在该文件夹下面创建spring.factories文件,文件里面配置监听类的全路径,第一行是springboot加载寻找的key,自己的按照如下方式配置即可

org.springframework.context.ApplicationListener=\
boot.Listener.MyListener,\
boot.Listener.DateListener
1
2
3
boot.Listener.MyListener

package boot.Listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

public class MyListener implements ApplicationListener<ApplicationEvent>{
@Override
public void onApplicationEvent(ApplicationEvent paramE) {
System.out.println("--------"+paramE.getClass().getName());
}
}

1
2
3
4
5
6
7
8
9
10
11
12
启动日志会记下所有的启动事件

--------**org.springframework.boot.context.event.ApplicationStartedEvent**
--------**org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent**

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.8.RELEASE)

2019-05-21 14:36:56.423 INFO 4120 --- [ main] boot.application : Starting application on LIUJP with PID 4120 (started by Administrator in E:\Program Files\wowkSpace\boot)
2019-05-21 14:36:56.435 INFO 4120 --- [ main] boot.application : No active profile set, falling back to default profiles: default
--------**org.springframework.boot.context.event.ApplicationPreparedEvent**
2019-05-21 14:36:56.718 INFO 4120 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@21a947fe: startup date [Tue May 21 14:36:56 CST 2019]; root of context hierarchy
2019-05-21 14:37:02.015 INFO 4120 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-05-21 14:37:02.062 INFO 4120 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-05-21 14:37:02.078 INFO 4120 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23
2019-05-21 14:37:02.624 INFO 4120 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-05-21 14:37:02.624 INFO 4120 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5922 ms
2019-05-21 14:37:03.218 INFO 4120 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-05-21 14:37:03.235 INFO 4120 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-05-21 14:37:03.251 INFO 4120 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-05-21 14:37:03.251 INFO 4120 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-05-21 14:37:03.251 INFO 4120 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-05-21 14:37:04.469 INFO 4120 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@21a947fe: startup date [Tue May 21 14:36:56 CST 2019]; root of context hierarchy
2019-05-21 14:37:04.725 INFO 4120 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2019-05-21 14:37:04.725 INFO 4120 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2019-05-21 14:37:04.835 INFO 4120 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-21 14:37:04.850 INFO 4120 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-21 14:37:04.991 INFO 4120 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-21 14:37:05.491 INFO 4120 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
--------**org.springframework.context.event.ContextRefreshedEvent**
2019-05-21 14:37:05.716 INFO 4120 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
--------**org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent**
--------**org.springframework.boot.context.event.ApplicationReadyEvent**
2019-05-21 14:37:05.732 INFO 4120 --- [ main] boot.application : Started application in 10.546 seconds (JVM running for 11.845)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
如上可以看出Springboot启动时间依次 是
org.springframework.boot.context.event.ApplicationStartedEvent
org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
org.springframework.boot.context.event.ApplicationPreparedEvent
org.springframework.context.event.ContextRefreshedEvent
org.springframework.boot.context.event.ApplicationReadyEvent

监听出处

SpringApplication.run(application.class, args);

SpringApplicationRunListeners listeners = getRunListeners(args);

private SpringApplicationRunListeners getRunListeners(String[] args) {
Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(
SpringApplicationRunListener.class, types, this, args));
}

private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type,
Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// Use names and ensure unique to protect against duplicates
Set<String> names = new LinkedHashSet<String>(
SpringFactoriesLoader.loadFactoryNames(type, classLoader));
List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
classLoader, args, names);
AnnotationAwareOrderComparator.sort(instances);
return instances;
}

public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";

public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
try {
Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
List<String> result = new ArrayList<String>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
String factoryClassNames = properties.getProperty(factoryClassName);
result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
}
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() +
"] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}

---------------------

Sprinboot优雅配置监听,并记录所有启动事件的更多相关文章

  1. 涂抹Oracle笔记1-创建数据库及配置监听程序

    一.安装ORACLE数据库软件及创建实例OLTP:online transaction processing 指那些短事务,高并发,读写频繁的数据库系统.--DB_BLOCK_SIZE通常设置较小.O ...

  2. oracle 11g 服务启动时提示1053错误,服务启动不了,重新配置监听解决问题

    早上发现oracle服务启动不了了,找了很多资料,没找到有用的.通过重新配置监听解决问题.

  3. 新建Oracle数据库时,提示使用database control配置数据库时,要求在当前oracle主目录中配置监听程序

    新建一个oracle数据库时,当提示使用database control配置数据库时,要求在当前oracle主目录中配置监听程序等字样的时候,问题是那个监听的服务没有启动,解决方法如下: 打开cmd命 ...

  4. 12C cdb/pdb 配置监听

    . PDB is not an instance, so using SID in the connection string will not work. When the database is ...

  5. spring中配置监听队列的MQ

    一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置.  <bean id="axx" ...

  6. Oracle 配置监听和本地网络服务

    一.配置监听 在oracle的配置和移植工具中打开Net Configuration Assistant,然后点击下一步. 点击下一步,然后输入监听的名称点击下一步 点击下一步后如图 点击下一步如图 ...

  7. oracle12安装软件后安装数据库,然后需要自己配置监听

    oracle12安装软件后安装数据库,然后需要自己配置监听 没想到你是这样的oracle12: 不能同时安装软件和数据库,分别安装之后,\NETWORD\ADMIN\下面竟然没有listener.or ...

  8. gradle 参数配置监听

    说明 gradle提供了对project状态配置监听的接口回调,以方便我们来配置一些Project的配置属性,监听主要分为两大类,一种是通过project进行 回调,一种是通过gradle进行回调,作 ...

  9. Oracle11g配置监听

    步骤 1.在windows系统上安装好Oracle后,点击右下角开始菜单Oracle目录下选择Net Manager进行配置,也可以使用Net Configuration Assistant(建议使用 ...

随机推荐

  1. [miniApp] WeChat user login code

    in client/app.js, we put user login logic inside here, so that other module can reuse those code by ...

  2. POJ_1679_The Unique MST(次小生成树模板)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23942   Accepted: 8492 D ...

  3. IIS2008配置URlRewriter

    一: 1:新增将要公布的站点:将自己主动生成的改站点的应用程序池的.net版本号更改为自己的版本号以及托管管道模式改成集成:如图 2:进入站点配置项里面的处理程序映射 watermark/2/text ...

  4. Unity编程笔录--ulua+PureMVC框架简单热更新使用

    ulua+PureMVC框架简单热更新使用 前言: 1:作者官网论坛 首先介绍的是这个框架是一位大牛  骏擎[CP]  jarjin   写的,据说原本是"非常多人不知道怎么使用Ulua,所 ...

  5. oc79--数组的内存管理

    // // main.m // 集合(数组)对象的内存管理(MRC中) // #import <Foundation/Foundation.h> #import "Person. ...

  6. Solution:Cannot pull with rebase: You have unstaged changes in Github

    You can do this to work around using following steps 1. stash your changes with: git stash 2. pull f ...

  7. iOS开发基础:OC数组对象NSArray的常用方法

    本文介绍了OC的数组对象的基本方法的使用: 因为OC的数组中存储的为对象类型,所以我们可以新建一个Person类,通过Person生成对象进行操作. 其中Person.h中的代码为: [objc] v ...

  8. Silverlight 2学习笔记一:初识Silverlight

    Silverlight,问世至今已有好一段时日了,向来只是只闻其名,不知其实,今天终于对Silverlight有了点初步的了解. 一.Silverlight是什么?Sliverlight是基于.NET ...

  9. Silverlight,Windows 8应用开发实例教程系列汇总

    Kevin Fan分享开发经验,记录开发点滴 Silverlight,Windows 8应用开发实例教程系列汇总 2012-06-18 01:05 by jv9, 2145 阅读, 3 评论, 收藏, ...

  10. 【POJ 3190】 Stall Reservations

    [题目链接] http://poj.org/problem?id=3190 [算法] 将这些牛按开始吃草的时间排序 维护一个数组S,Si表示畜栏i进去的最后一头牛结束吃草的时间,对于每头牛,找任意一个 ...