SpringBoot项目非web方式启动

1、springboot 1.x中以非web方式启动 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// 启动方式1<br>SpringApplication app = new SpringApplication(Application.class);<br>app.setWebEnvironment(false);// 设置ApplicationContext类型<br>ApplicationContext ctx = app.run(args);// 启动方式2<br>@SpringBootApplication 

public class Application implements ApplicationRunner{ 

   

    public static void main(String[] args) { 

        new SpringApplicationBuilder() 

            .sources(Application.class).web(false).run(args); 

    

   

    @Override 

    public void run(ApplicationArguments args) throws Exception { 

        while(true) { 

            System.out.println("now is " new Date().toLocaleString()); 

        Thread.sleep(1000); 

    

    

   

2、springboot 2.0中以非web方式启动 

-web(false)/setWebEnvironment(false) is deprecated and instead Web-Application-Type can be used to specify 

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

# 配置spring.main.web-application-type=NONE# 代码@SpringBootApplication 

public class Application implements ApplicationRunner{ 

   

    public static void main(String[] args) { 

        new SpringApplicationBuilder(Application.class

            .web(WebApplicationType.NONE) // .REACTIVE, .SERVLET 

            .bannerMode(Banner.Mode.OFF) 

            .run(args); 

    

   

    @Override 

    public void run(ApplicationArguments args) throws Exception { 

        while(true) { 

            System.out.println("now is " new Date().toLocaleString()); 

            Thread.sleep(1000); 

        

    

       

}

你可能感兴趣的