Java: Spring Boot不使用Web服务器执行代码

java标志
image-3303

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Spring3DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        var spring=new SpringApplication(Spring3DemoApplication.class);
        spring.setWebApplicationType(WebApplicationType.NONE);
        spring.run(args);
    }


    @Override
    public void run(String... args) {
        // 你的代码
    }
}