ApplicationListener
属于Spring框架对Java中实现的监听者模式的一种框架实现,这里唯一值得着重强调的是,对于初次接触SpringBoot,但对Spring框架本身又没有过多接触的开发者来说,可能会将这个名字与SpringApplicationRunListener混淆。
如果我们要为SpringBoot应用添加自定义的ApplicationListener,有两种方式:
1)通过SpringApplication.addListeners
(…)或者SpringApplication.setListeners
(…)方法添加一个或者多个自定义的ApplicationListener;
2)借助SpringFactoriesLoader机制,在META-INF/spring.factories文件中添加配置(以下代码是为SpringBoot默认注册的ApplicationListener配置):
org.springframework.context.ApplicationListener=\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.cloudfoundry.VcapApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicat-ionListener,\
org.springframework.boot.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.logging.LoggingApplicationListener