Spring Boot Spring的CommandLineRunner未执行

lx0bsm1f  于 2022-11-05  发布在  Spring
关注(0)|答案(1)|浏览(1180)

我想测试CommandLineRunner,但我无法让它工作。我只有2个类:

  1. FrameworktestApplication.java
    软件包com.caido.框架测试;
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);
     }

 }

1.命令行运行程序测试:
软件包com.caido.框架测试;

import org.springframework.boot.CommandLineRunner;

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

当我运行应用程序时,我在屏幕上看不到“Start CommandLineRunner”。这是完整的输出:

--- 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)
fruv7luv

fruv7luv1#

您应该通过注解@Component将CommandLineRunnerTest类标记为Spring Bean。
CommandLineRunner接口只是在所有Spring应用程序上下文启动并运行后添加到Spring Bean的“running”特性(执行方法“run”)。

相关问题