One of the common things on web application development is to initialize or invoke a piece of code at the start up of the application. If you are developing a traditional Java web application, the startup logic can be mentioned in the ServletContextListener. Spring also offers very convenient way of invoking the events at the different stages of the application life cycle. I have earlier explained about the EventListener in Spring 4 and ContextRefreshedEvent.
This tutorial guides you through on implementing the start up event listeners when writing the spring boot applications.
Spring Framework Events
The following are the list of spring framework events that can be triggered at the spring context initialization.
- ContextRefreshedEvent : This event is triggered upon spring context start and refresh events. This events is most commonly used for initializing the data at the start up.
- ContextStartedEvent : This event is triggered upon spring context start. The main difference between ContextRefreshedEvent is that this event is invoked only at the start and not on the refresh of context.
- ContextStopedEvent : This event is triggered when spring context is stopped.
- ContextClosedEvent : This event is triggered when spring context is closed.
Spring Boot Events Example
I have written a simple example that demonstrates how to publish an event at the spring boot application startup. I have registered ApplicationListener for listening to the ContextRefreshedEvent. Let’s look at the code for more understanding.
I have uploaded the complete source code for the download.
ContextRefreshedListener.java
package net.javabeat.spring.boot.example.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; @Component public class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> { private SampleEvent sampleEvent; @Autowired public void setSampleEvent(SampleEvent sampleEvent) { this.sampleEvent = sampleEvent; } public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { System.out.println("Context Refreshed"); sampleEvent.setEventFired(true); } }
SampleEvent.java
package net.javabeat.spring.boot.example.util; import org.springframework.stereotype.Component; @Component public class SampleEvent { private Boolean eventFired = false; public Boolean getEventFired() { return eventFired; } public void setEventFired(Boolean eventFired) { this.eventFired = eventFired; } }
Application.java
package net.javabeat.spring.boot.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import net.javabeat.spring.boot.example.util.SampleEvent; @SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args); SampleEvent bean = ctx.getBean(SampleEvent.class); System.out.println("Is Event Fired : " + bean.getEventFired()); } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.javabeat</groupId> <artifactId>boot</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>boot</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>boot</finalName> </build> </project>
Output for the above program is:
2016-03-18 22:25:15.783 INFO 6636 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup Context Refreshed 2016-03-18 22:25:15.972 INFO 6636 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2016-03-18 22:25:15.980 INFO 6636 --- [ main] n.j.spring.boot.example.Application : Started Application in 2.699 seconds (JVM running for 3.003) Is Event Fired : true