Executing Code on Startup of Spring Boot Application



In this tutorial, we will find how to execute a block of code on application startup.

Developers frequently need some startup code that needs to be executed once the application starts. This can be reading a set of configuration values from the database which will remain constant or execute some notification logic. Spring boot provides ApplicationRunner and CommandLineRunner interfaces to accomplish this task. 

Spring boot also provides ApplicationArguments class which can be injected into any class that needs program argument values.

Find this project on Github

https://github.com/javacodenet/spring-boot-application-startup-code

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javacodenet</groupId>
<artifactId>spring-boot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub

Application.java

package com.javacodenet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class Application {
@Autowired
ApplicationArguments applicationArguments;
@RequestMapping("/")
public String programArguments() {
String output = "Application Startup Program arguments:";
for (String arg: applicationArguments.getOptionNames()) {
output = output + " " + arg;
}
return output;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

MyCommandLineRunner.java

package com.javacodenet;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommandLineRunner implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
System.out.println("Application Startup Program arguments");
for (String arg: args) {
System.out.println(arg);
}
}
}

MyApplicationRunner.java

package com.javacodenet;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationRunner implements ApplicationRunner{
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("Application Startup Program arguments");
for (String arg: args.getOptionNames()) {
System.out.println(arg);
}
}
}

Program arguments added


Output from MyCommandLineRunner


Output from MyApplicationRunner


Rest End Point Output


Comments

Popular posts from this blog

Generate PDF From XML Or Java Object Using Apache FOP

Generate Random values in Spring boot

Generate/Read an Excel file in Java using Apache POI