前言
当我们使用Spring Boot编写了一个批处理应用程序,该程序只是用于后台跑批数据,此时不需要内嵌的tomcat,简化启动方式使用非web方式启动项目。
执行步骤
1.修改pom.xml文件
在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应用程序,请在属性文件中禁用Web应用程序类型,application.yml文件中添加
spring:
main:
web-application-type: none
4.在启动类中进行扩展
继承SpringBootServletInitializer 类,以下本人写了一个测试方法,项目启动后生成一个txt文件进行测试
@SpringBootApplicationpublicclassTestiopojApplicationextendsSpringBootServletInitializer{publicstaticvoidmain(String[] args){
System.out.println("项目开始启动,开始执行任务============");
SpringApplication.run(TestiopojApplication.class, args);
String file="E:\\copyFile";//文件存放路径
String fileName="test测试";//生成的文件名
String strContext="测试成功=======";//文件内容try{
FileUtils.writeStringToFile((newFile(file+ File.separator+ fileName+".txt")), strContext,"UTF-8");
System.out.println("文件创建成功============");}catch(IOException e){
System.out.println("文件创建失败============");}}}
5.实列测试结果
由此我们可以通过java -jar 运行打包后的项目jar,控制台显示Spring Boot启动标志,项目正常启动,文件也正常创建成功,大功告成!
文章参考:点我点我点我
总结
提示: 我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!