Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot CommandLineRunner run() method not being called

I'm learning SpringBoot. My (trivial) problem is I can't get the run() method from the CommandLineRunner Interface to be called by SpringBoot. I'm using Java 8 , Eclipse Oxygen ,Maven and i'm running my project as a "Spring Boot App". Code is:

package com.clarivate.dataviewer;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DvMain implements CommandLineRunner{

static Logger logger = LogManager.getRootLogger();

public static void main(String[] args) {
    logger.debug("DS1A in main()");
    //SpringApplication.run(DvMain.class, args);
    SpringApplication app = new SpringApplication(DvMain.class);
    //ConfigurableApplicationContext app = SpringApplication.run(DvMain.class, args);
    logger.debug("DS1B in main()");
    app.run(args);
}

@Override
public void run(String... args) throws Exception {
    // This does not get called
    logger.debug("DS4 this line is never printed");
}
}

The overridden run() method does not get called. I've tried creating a separate class that implements CommandLineRunner but same problem. The console trace is:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/44/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/44/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.10.0/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
12:58:42.225 [main] DEBUG  - DS1A in main()
12:58:42.289 [main] DEBUG  - DS1B in main()

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

2018-06-20 12:58:42.614  INFO 16924 --- [           main] com.clarivate.dataviewer.DvMain          : Starting DvMain on 44-TPD-A with PID 16924 (C:\workspace_oxyegen\dataviewer\target\classes started by 44 in C:\workspace_oxyegen\dataviewer)
2018-06-20 12:58:42.615  INFO 16924 --- [           main] com.clarivate.dataviewer.DvMain          : No active profile set, falling back to default profiles: default
2018-06-20 12:58:42.655  INFO 16924 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6b4a4e18: startup date [Wed Jun 20 12:58:42 BST 2018]; root of context hierarchy
2018-06-20 12:58:43.266  INFO 16924 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-06-20 12:58:43.279  INFO 16924 --- [           main] com.clarivate.dataviewer.DvMain          : Started DvMain in 0.988 seconds (JVM running for 1.684)
2018-06-20 12:58:43.294  INFO 16924 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6b4a4e18: startup date [Wed Jun 20 12:58:42 BST 2018]; root of context hierarchy
2018-06-20 12:58:43.296  INFO 16924 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

I'm sure i've made a simple mistake but I just can't see it. Any help appreciated.

like image 229
DS. Avatar asked Jun 20 '18 12:06

DS.


People also ask

What happens when SpringApplication run is called?

Spring Boot SpringApplication class is used to bootstrap and launch a Spring application from a Java main method. This class automatically creates the ApplicationContext from the classpath, scan the configuration classes and launch the application.

What is the use of CommandLineRunner in Spring Boot?

CommandLineRunner is a simple Spring Boot interface with a run method. Spring Boot will automatically call the run method of all beans implementing this interface after the application context has been loaded.

How do I run Spring Boot from main method?

A Spring Boot application's main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn't explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.


1 Answers

Firstly, the call is coming to your run method. But doesn't log anything because there are couple of issues with your logger. You can use System.out.println(...) to confirm this.

  1. Change your Logger declaration as below.

    private static final Logger logger = LoggerFactory.getLogger(DvMain.class);
    
  2. You are not using correct libraries. Use Logger from slf4j and not log4j

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
like image 173
pvpkiran Avatar answered Oct 19 '22 15:10

pvpkiran