Home > OS >  Spring's CommandLineRunner is not executing
Spring's CommandLineRunner is not executing

Time:10-31

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

  1. FrameworktestApplication.java

    package com.caido.frameworktest;

     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)

CodePudding user response:

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.

  • Related