• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Firing Events at Spring Boot Startup

March 18, 2016 //  by Krishna Srinivasan//  Leave a Comment

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.

Spring Boot Startup Events
Spring Boot Startup Events

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

Download Source Code

Icon

Spring Boot Events

1 file(s) 3.95 KB
Download

Category: Spring FrameworkTag: Spring Boot Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: «Spring Boot Configurations @EnableAutoConfiguration Annotation in Spring Boot
Next Post: @RequestMapping Annotation in Spring MVC New Features in Spring Boot 1.4»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact