我想测试CommandLineRunner,但我无法让它工作。我只有两门课:
包装com.caido.frameworktest;
导入org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication公共类FrameworktestApplication {公共静态空主(String[] args) {System.out.println(“开始运行”);}
包装com.caido.frameworktest;
导入org.springframework.boot.CommandLineRunner;公共类CommandLineRunnerTest实现CommandLineRunner { @Override (字符串.)抛出异常{ System.err.println("Start CommandLineRunner");}
当我运行这个应用程序时,我看到屏幕上没有“启动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)发布于 2022-10-30 19:28:24
您应该通过注释@Component将您的CommandLineRunnerTest类标记为Spring。
CommandLineRunner接口只是在所有Spring上下文启动和运行之后,添加到Spring特性的“运行”(执行方法"run")。
https://stackoverflow.com/questions/74255958
复制相似问题