springboot-事件
@SpringBootApplication public class SpringbootdemoApplication { public static void main(String[] args) { new SpringApplicationBuilder(SpringbootdemoApplication.class) .listeners(event -> { System.out.println("监听到事件:" + event.getClass().getName()); }) .run(args) .close(); } }
输出结果:
监听到事件:org.springframework.boot.context.event.ApplicationStartingEvent(1)
监听到事件:org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent(2)监听到事件:org.springframework.boot.context.event.ApplicationContextInitializedEvent
监听到事件:org.springframework.boot.context.event.ApplicationPreparedEvent(3)
监听到事件:org.springframework.context.event.ContextRefreshedEvent
监听到事件:org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent
监听到事件:org.springframework.boot.context.event.ApplicationStartedEvent(4)
监听到事件:org.springframework.boot.context.event.ApplicationReadyEvent(5)
监听到事件:org.springframework.context.event.ContextClosedEvent
(6) ApplicationFailedEvent 特殊情况
以上6种是springboot的事件。
ConfigFileApplicationListener监听ApplicationEnvironmentPreparedEvent事件,从而加载application.properties或
application.yml文件
springboot很多组件依赖于springboot事件监听器实现,本质是spring framework事件监听机制
SpringApplication利用
spring应用上下文(ApplicationContext)生命周期控制注解驱动bean
spring事件/监听机制(ApplicationEventMulticaster)加载或初始化组件