1. Overview

spring boot application startup listener or init Method called when spring application will start. It will be called only once in spring boot application cycle. Sometimes we need to execute some code at starting of spring boot application. We can archive using ApplicationRunner or CommandRunner

1. ApplicationRunner Example

package com.javadeveloperzone;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Created by JavaDeveloperZone on 27-07-2017.
 */
@Component
@Order(value = 1)
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("init application");
    }
}

Output:

Here is output which prints a simple line in the console after spring application will be started it will be called ApplicationRunner. @Order annotation will be used when more than one applicationRunners are in an application when want to maintain the sequence of ApplicationRunner.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)


2017-07-27 19:57:19.943  INFO 17908 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-07-27 19:57:20.003  INFO 17908 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
init application
2017-07-27 19:57:20.007  INFO 17908 --- [           main] com.javadeveloperzone.SpringBootConfig   : Started SpringBootConfig in 3.674 seconds (JVM running for 4.254)

2. CommandLineRunner Example

CommandLineRunner will be called by spring boot application one application stated successfully here is an example of CommandLineRunner

package com.javadeveloperzone;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Created by JavaDeveloperZone on 27-07-2017.
 */
@Component
@Order(value = 1)
public class MyCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String[] args) throws Exception {
        System.out.println("init application using command line Runner");
    }
}

Output:

Here is output where in console application will be called MyCommandRunner

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)


2017-07-27 20:02:46.219  INFO 42812 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
init application using command line Runner
2017-07-27 20:02:46.219  INFO 42812 --- [           main] com.javadeveloperzone.SpringBootConfig   : Started SpringBootConfig in 4.167 seconds (JVM running for 4.732)

 

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

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