Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring's CommandLineRunner is not executing

I want to test CommandLineRunner and I can’t make it work.
I have just 2 classes:

  1. FrameworktestApplication.java

    package com.caido.frameworktest;

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;
    
     @SpringBootApplication
     public class FrameworktestApplication  {
         public static void main(String[] args) {
             System.out.println("Start run");
             SpringApplication.run(FrameworktestApplication.class, args);
         }
    
     }
    
  2. CommandLineRunnerTest:

    package com.caido.frameworktest;

     import org.springframework.boot.CommandLineRunner;
    
     public class CommandLineRunnerTest implements CommandLineRunner {
         @Override
         public void run(String... strings) throws Exception {
             System.err.println("Start CommandLineRunner");
         }
     }
    

When I run the app I see no "Start CommandLineRunner" on the screen.
This is the full output:

--- exec-maven-plugin:3.0.0:exec (default-cli) @ frameworktest ---
Start run

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

2022-10-30 20:53:59.862  INFO 14332 --- [           main] c.c.f.FrameworktestApplication           : Starting FrameworktestApplication using Java 19.0.1 on DESKTOP-J30M0PF with PID 14332 (Y:\Caido\Dev\test\frameworktest\target\classes started by victor in Y:\Caido\Dev\test\frameworktest)
2022-10-30 20:53:59.866  INFO 14332 --- [           main] c.c.f.FrameworktestApplication           : No active profile set, falling back to 1 default profile: "default"
2022-10-30 20:54:01.134  INFO 14332 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-10-30 20:54:01.148  INFO 14332 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-10-30 20:54:01.149  INFO 14332 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.68]
2022-10-30 20:54:01.256  INFO 14332 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-10-30 20:54:01.256  INFO 14332 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1327 ms
2022-10-30 20:54:01.720  INFO 14332 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-10-30 20:54:01.729  INFO 14332 --- [           main] c.c.f.FrameworktestApplication           : Started FrameworktestApplication in 2.503 seconds (JVM running for 2.906)

>Solution :

You should mark your CommandLineRunnerTest class as Spring Bean by annotation @Component.

CommandLineRunner interface just add to Spring Bean feature of "running" (executing of method "run") after all Spring Application Context is up and running.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading