其他
纳尼?Spring Boot 项目中使用 CommandLineRunner 有坑!
>>号外:关注“Java精选”公众号,回复“2021面试题”,领取免费资料!“Java精选面试题”小程序,3000+ 道面试题在线刷,最新、最全 Java 面试题!
使用场景
两个接口的不同
特殊的场景
遇到的坑
2018-07-16 01:48:22.378 INFO 9164 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-07-16 01:48:22.386 INFO 9164 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2018-07-16 01:48:22.386 INFO 9164 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed2018-07-16 01:48:22.396 INFO 9164 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2018-07-16 01:48:22.417 INFO 9164 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references2018-07-16 01:48:22.546 INFO 9164 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8088 (http)
2018-07-16 01:48:22.555 INFO 9164 --- [ main] com.hello.word.WordParseApplication : Started WordParseApplication in 3.811 seconds (JVM running for 4.486)
/**
* @author zhangwq
* @date 2018/7/16 1:36
*/
@Component
public class RunService implements CommandLineRunner {
public void run(String... strings){
int i =0;
while(true){
i++;
try {
Thread.sleep(10000);
System.out.println("过去了10秒钟……,i的值为:"+i);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i==4){ //第40秒时抛出一个异常
throw new RuntimeException();
}
continue;
}
}
}
2018-07-16 01:56:43.703 INFO 7424 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2018-07-16 01:56:43.703 INFO 7424 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2018-07-16 01:56:43.722 INFO 7424 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2018-07-16 01:56:43.750 INFO 7424 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
2018-07-16 01:56:43.885 INFO 7424 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8088 (http)
过去了10秒钟……,i的值为:1
过去了10秒钟……,i的值为:2
过去了10秒钟……,i的值为:3
过去了10秒钟……,i的值为:4
2018-07-16 01:57:23.939 INFO 7424 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-07-16 01:57:23.973 ERROR 7424 --- [ main] o.s.boot.SpringApplication : Application startup failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:304) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at com.hello.word.WordParseApplication.main(WordParseApplication.java:15) [classes/:na]
Caused by: java.lang.RuntimeException: null
at com.zhangwq.service.RunService.run(RunService.java:24) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
... 6 common frames omitted
2018-07-16 01:57:23.975 INFO 7424 --- [ main] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@14a4e18: startup date [Mon Jul 16 01:56:39 CST 2018]; root of context hierarchy
2018-07-16 01:57:23.975 INFO 7424 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 2147483647
2018-07-16 01:57:23.975 INFO 7424 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Process finished with exit code 1
填坑
/**
* @author zhangwq
* @date 2018/7/16 1:36
*/
@Component
public class RunService implements CommandLineRunner {
public void run(String... strings){
new Thread(){
public void run() {
int i = 0;
while (true) {
i++;
try {
Thread.sleep(10000);
System.out.println("过去了10秒钟……,i的值为:" + i);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i == 4) { //第40秒时抛出一个异常
throw new RuntimeException();
}
continue;
}
}
}.start();
}
}
2018-07-16 02:05:52.680 INFO 7148 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2018-07-16 02:05:52.680 INFO 7148 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2018-07-16 02:05:52.695 INFO 7148 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2018-07-16 02:05:52.717 INFO 7148 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
2018-07-16 02:05:52.815 INFO 7148 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8088 (http)
2018-07-16 02:05:52.819 INFO 7148 --- [ main] com.hello.word.WordParseApplication : Started WordParseApplication in 3.406 seconds (JVM running for 4.063)
过去了10秒钟……,i的值为:1
过去了10秒钟……,i的值为:2
过去了10秒钟……,i的值为:3
过去了10秒钟……,i的值为:4
Exception in thread "Thread-10" java.lang.RuntimeException
at com.zhangwq.service.RunService$1.run(RunService.java:26)
作者:狮子头儿
blog.csdn.net/zwq_zwq_zwq/article/details/81059017
为什么数据库连接池要用 ThreadLocal 呢?不用会怎么样?
Java 程序员不可或缺的 11 种 IntelliJ IDEA 插件,提升开发效率!
坑你没商量!盘点 JDK 中最常见的“翻车”现场,你都踩过那些坑?
Java 中 Service 层和 DAO 层有必要每个类都加上接口吗?
美团实习面试:熟悉红黑树是吧?能不能写一下?说一说空间和时间复杂度?
Spring Boot 集成 WebSocket,实现前后端即时通讯,如此简单!
Spring Boot 接口频繁超时,Alibaba 开源 Arthas 精准定位 BUG 问题!
踩坑!Spring 事务方法与非事务方法相互调用 @Transactional 注解失效不回滚?