当使用spring-boot-starter-web时,spring-boot应用程序在启动后不会退出。
使用Jetbrain IDEA,有一个图标显示Spring启动完成:
但如果我使用:
public static void main ( String[] args ){
SpringApplication.run(Application.class, args);
Thread.sleep(Long.MAX_VALUE);
}
或
public class MyRunner implements ApplicationRunner{
public void run() {
Thread.sleep(Long.MAX_VALUE);
}
}
可以让spring-boot继续运行,但IDEA图标将永远加载,所以与入门网站相比,这一定是不同的方式。
更新1:这两种方法将导致SpringBootTest永远等待
问题:spring-boot-starter-web阻止spring-boot退出的代码是什么?
我不知道是什么原因,但如果没有Web服务器,只有当任何其他线程(非守护进程线程)仍处于活动状态时,才能防止关闭。另一个线程中的无限循环将阻止程序退出。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
System.out.println("main started");
new Thread(() -> {
System.out.println("thread started");
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
while (true) {
System.out.println(context);
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
}
}).start();
System.out.println("main finished");
}
}
输出示例
main started
main finished
thread started
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.7)
2022-06-24 22:34:31.648 INFO 8084 --- [ Thread-1] o.s.boot.SpringApplication : Starting application using Java 1.8.0_312 on DESKTOP with PID 8084 (started by user in C:\Users\user\IdeaProjects\demo)
2022-06-24 22:34:31.650 INFO 8084 --- [ Thread-1] o.s.boot.SpringApplication : No active profile set, falling back to 1 default profile: "default"
2022-06-24 22:34:31.985 INFO 8084 --- [ Thread-1] org.quartz.impl.StdSchedulerFactory : Using default implementation for ThreadExecutor
2022-06-24 22:34:31.991 INFO 8084 --- [ Thread-1] org.quartz.core.SchedulerSignalerImpl : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2022-06-24 22:34:31.991 INFO 8084 --- [ Thread-1] org.quartz.core.QuartzScheduler : Quartz Scheduler v.2.3.2 created.
2022-06-24 22:34:31.991 INFO 8084 --- [ Thread-1] org.quartz.simpl.RAMJobStore : RAMJobStore initialized.
2022-06-24 22:34:31.992 INFO 8084 --- [ Thread-1] org.quartz.core.QuartzScheduler : Scheduler meta-data: Quartz Scheduler (v2.3.2) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
2022-06-24 22:34:31.992 INFO 8084 --- [ Thread-1] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2022-06-24 22:34:31.992 INFO 8084 --- [ Thread-1] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.3.2
2022-06-24 22:34:31.992 INFO 8084 --- [ Thread-1] org.quartz.core.QuartzScheduler : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@501627dc
2022-06-24 22:34:32.009 INFO 8084 --- [ Thread-1] o.s.s.quartz.SchedulerFactoryBean : Starting Quartz Scheduler now
2022-06-24 22:34:32.010 INFO 8084 --- [ Thread-1] org.quartz.core.QuartzScheduler : Scheduler quartzScheduler_$_NON_CLUSTERED started.
2022-06-24 22:34:32.015 INFO 8084 --- [ Thread-1] o.s.boot.SpringApplication : Started application in 0.601 seconds (JVM running for 1.051)
org.springframework.context.annotation.AnnotationConfigApplicationContext@74ed4e3, started on Fri Jun 24 22:34:31 MSK 2022
org.springframework.context.annotation.AnnotationConfigApplicationContext@74ed4e3, started on Fri Jun 24 22:34:31 MSK 2022
org.springframework.context.annotation.AnnotationConfigApplicationContext@74ed4e3, started on Fri Jun 24 22:34:31 MSK 2022
and so on...
只有 1 个依赖项(只是随机启动器,不是 Web,这里不需要石英)
org.springframework.boot:spring-boot-starter-quartz
只是非守护线程中的无限循环...
回答问题
Spring启动启动器网络阻止Spring启动退出的代码是什么
spring-boot-web 创建 Web 服务器 bean,它使用非守护进程侦听器线程(因为您希望它们处于活动状态并运行以等待传入连接并处理它们)。
这使得您的代码到达< code>main方法的末尾,但是JVM不会退出,因为它有非守护线程在运行。
如果你想让你的应用程序保持活力,你也需要这样做——最简单的方法是在一个bean中创建一个线程,就像@Slongtong建议的那样。