Thursday, February 12, 2015

Spring Boot event flow

The following logs show a event flow in a Spring Boot web application:

SpringApplicationEvent: ApplicationStartedEvent
SpringApplicationEvent: ApplicationEnvironmentPreparedEvent
SpringApplicationEvent: ApplicationPreparedEvent
Application$$EnhancerBySpringCGLIB$$b8c60715: @PostConstruct
SomeRepository: @PostConstruct
SomeService: @PostConstruct
SomeController: @PostConstruct
ApplicationEvent: ContextRefreshedEvent
ApplicationEvent: EmbeddedServletContainerInitializedEvent
ApplicationEvent: ServletRequestHandledEvent
...
ApplicationEvent: ServletRequestHandledEvent
ApplicationEvent: ContextClosedEvent
SomeController: @PreDestroy
SomeService: @PreDestroy
SomeRepository: @PreDestroy
Application$$EnhancerBySpringCGLIB$$b8c60715: @PreDestroy

ApplicationEvent is a Spring event

and SpringApplicationEvent is a Spring Boot event.

You can define your event handlers by implementing ApplicationListener.

But note that Spring Boot events can't be caught by ApplicationListener beans

because all of them are fired before creating the beans.

Use SpringApplication.addListeners() for Spring Boot events instead.

You can find this sample project in the following link:

https://github.com/izeye/samples-spring-boot-branches/tree/event

---

Source-added versions:

Web:

SpringApplicationEvent: ApplicationStartedEvent from org.springframework.boot.SpringApplication@5bcab519
SpringApplicationEvent: ApplicationEnvironmentPreparedEvent from org.springframework.boot.SpringApplication@5bcab519
SpringApplicationEvent: ApplicationPreparedEvent from org.springframework.boot.SpringApplication@5bcab519
Application$$EnhancerBySpringCGLIB$$29fbb36b: @PostConstruct
SomeRepository: @PostConstruct
SomeService: @PostConstruct
SomeController: @PostConstruct
ApplicationEvent: ContextRefreshedEvent from org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1990a65e: startup date [Mon Mar 02 21:04:17 KST 2015]; root of context hierarchy
ApplicationEvent: EmbeddedServletContainerInitializedEvent from org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer@7882c44a
ApplicationEvent: ServletRequestHandledEvent from org.springframework.web.servlet.DispatcherServlet@2990747b
...
ApplicationEvent: ContextClosedEvent from org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1990a65e: startup date [Mon Mar 02 20:42:31 KST 2015]; root of context hierarchy
SomeController: @PreDestroy
SomeService: @PreDestroy
SomeRepository: @PreDestroy
Application$$EnhancerBySpringCGLIB$$29fbb36b: @PreDestroy

Non-web:

SpringApplicationEvent: ApplicationStartedEvent from org.springframework.boot.SpringApplication@5b275dab
SpringApplicationEvent: ApplicationEnvironmentPreparedEvent from org.springframework.boot.SpringApplication@5b275dab
SpringApplicationEvent: ApplicationPreparedEvent from org.springframework.boot.SpringApplication@5b275dab
Application$$EnhancerBySpringCGLIB$$de573e5a: @PostConstruct
SomeRepository: @PostConstruct
SomeService: @PostConstruct
ApplicationEvent: ContextRefreshedEvent from org.springframework.context.annotation.AnnotationConfigApplicationContext@4923ab24: startup date [Mon Mar 02 20:52:40 KST 2015]; root of context hierarchy
...
ApplicationEvent: ContextClosedEvent from org.springframework.context.annotation.AnnotationConfigApplicationContext@4923ab24: startup date [Mon Mar 02 20:52:40 KST 2015]; root of context hierarchy
SomeService: @PreDestroy
SomeRepository: @PreDestroy
Application$$EnhancerBySpringCGLIB$$de573e5a: @PreDestroy

Reference:
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-application-events-and-listeners

No comments:

Post a Comment