好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Boot如何移除内嵌Tomcat,使用非web方式启动

前言:当我们使用Spring Boot编写了一个批处理应用程序,该程序只是用于后台跑批数据,此时不需要内嵌的tomcat,简化启动方式使用非web方式启动项目,步骤如下: 

1、修改pom.xml文件

在pom.xml文件中去除内嵌tomcat,添加servlet依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

< dependency >

       < groupId >org.springframework.boot</ groupId >

       < artifactId >spring-boot-starter-web</ artifactId >

       < exclusions >

         <!--去除内嵌tomcat -->

         < exclusion >

           < groupId >org.springframework.boot</ groupId >

           < artifactId >spring-boot-starter-tomcat</ artifactId >

         </ exclusion >

       </ exclusions >

     </ dependency >

     <!--添加servlet的依赖-->

     < dependency >

       < groupId >javax.servlet</ groupId >

       < artifactId >javax.servlet-api</ artifactId >

       < version >3.1.0</ version >

       < scope >compile</ scope >

     </ dependency >

2、设置打包方式

在pom.xml文件中将打项目包方式设置成jar,打成jar包通过命令去执行jar

?

1

< packaging >jar</ packaging >

3、禁用web程序启动方式

对于非Web应用程序,请在属性文件中禁用Web应用程序类型,application.yml文件中添加:

?

1

2

3

spring:

   main:

    web-application- type : none

4、在启动类中扩展

继承SpringBootServletInitializer 类,以下本人写了一个测试方法,项目启动后生成一个txt文件进行测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@SpringBootApplication

public class TestiopojApplication extends SpringBootServletInitializer {

 

   public static void main(String[] args) {

     System.out.println( "项目开始启动,开始执行任务============" );

     SpringApplication.run(TestiopojApplication. class , args);

     String file = "E:\\copyFile" ; //文件存放路径

     String fileName = "test测试" ; //生成的文件名

     String strContext = "测试成功=======" ; //文件内容

     try {

       FileUtils.writeStringToFile(( new File(file + File.separator + fileName + ".txt" )), strContext, "UTF-8" );

       System.out.println( "文件创建成功============" );

     } catch (IOException e) {

       System.out.println( "文件创建失败============" );

     }

   }

 

}

5、实列测试结果

由此我们可以通过java -jar 运行打包后的项目jar,控制台显示Spring Boot启动标志,项目正常启动,文件也正常创建成功,大功告成

以上就是Spring Boot如何移除内嵌Tomcat,使用非web方式启动的详细内容,更多关于Spring Boot移除内嵌Tomcat的资料请关注其它相关文章!

原文链接:https://HdhCmsTestcnblogs测试数据/bgyb/p/14452396.html

查看更多关于Spring Boot如何移除内嵌Tomcat,使用非web方式启动的详细内容...

  阅读:24次