bgyb

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

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

       <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

<packaging>jar</packaging>

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

spring:
    main:
      web-application-type: none

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

@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、实列测试结果

 

 

 

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

 

文章参考:点我点我点我

个人总结:

我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!

 

相关文章:

  • 2021-10-18
  • 2022-03-10
  • 2021-12-20
  • 2021-08-11
  • 2022-01-13
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2021-06-01
  • 2021-11-14
  • 2022-12-23
  • 2021-11-19
  • 2021-08-15
  • 2021-08-21
  • 2021-09-23
相关资源
相似解决方案