本地代码启动不报错,部署到服务器之后出现如下一个错误。

系统的日志如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. [o.s.b.a.l.ConditionEvaluationReportLoggingListener]
2019-08-15 15:57:51.789 [restartedMain] ERROR Application run failed [org.springframework.boot.SpringApplication]
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.hvisen.springboot.ApplicationLauncher.main(ApplicationLauncher.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalStateException: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@65f0c43a has been closed already
at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1067)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082)
at com.hvisen.springboot.utils.SpringUtils.getBean(SpringUtils.java:26)
at com.hvisen.springboot.ApplicationLauncher.run(ApplicationLauncher.java:28)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813)
... 10 common frames omitted

程序在启动之后,自动退出了。

问题分析:
对于应用来说,无需监听,在执行完任务之后,就自动退出,这个是非常正常的情况。那该如何做才能在后台让其一直运行呢?
办法也很简单,就是利用Thread.join()功能来实现,让主线程等待当前线程的退出,当前线程一直执行,不自动退出,则可以形成一个监听状态的应用。

解决代码如下:

import com.hvisen.springboot.base.TaskStarter;
import com.hvisen.springboot.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application implements CommandLineRunner { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} @Override
public void run(String[] args) throws Exception {
Thread.currentThread().join();
//需要启动的代码逻辑
}
}

Spring Boot Application后台守护Daemon应用的更多相关文章

  1. Inspection info: Checks Spring Boot application .properties configuration files. Highlights unresolved and deprecated configuration keys and in

    Cannot resolve class or package ‘jdbc’ less… (Ctrl+F1) Inspection info: Checks Spring Boot applicati ...

  2. SpringBoot零XML配置的Spring Boot Application

    Spring Boot 提供了一种统一的方式来管理应用的配置,允许开发人员使用属性properties文件.YAML 文件.环境变量和命令行参数来定义优先级不同的配置值.零XML配置的Spring B ...

  3. 【转】spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  4. spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500:no convertter for return value of type

    问题描述 spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson spring boot 新增配置解决后台返回 json 到前台中文乱码之后,出现返回json ...

  5. Spring boot application.properties 配置

    原文链接: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.ht ...

  6. spring boot项目后台运行

    spring boot项目后台运行 Spring Boot应用的几种运行方式: (1)运行Spring Boot的应用主类 (2)使用Maven的Spring Boot插件mvn spring-boo ...

  7. spring boot application.properties 属性详解

    2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...

  8. spring boot application properties配置详解

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  9. Spring Boot Application

    spring boot默认已经配置了很多环境变量,例如,tomcat的默认端口是8080,项目的contextpath是“/”等等,spring boot允许你自定义一个application.pro ...

随机推荐

  1. 最简单webview跳转

    String url = "http://www.qq.com" Uri uri=Uri.parse("http://www.baidu.com"); Inte ...

  2. SpringBoot 如何实现自动配置

    SpringMVC 和 SpringBoot 都是基于Spring的,两者推出的时间相差不大,只不过是SpringMVC推出早点. 关于两者,最近看到一个比较通俗的讲法: Spring 最初利用“工厂 ...

  3. 流媒体服务器搭建 red5

    简介 1. 流媒体指以流方式在网络中传送音频.视频和多媒体文件的媒体形式.相对于下载后观看的网络播放形式而言,流媒体的典型特征是把连续的音频和视频信息压缩后放到网络服务器上,用户边下载边观看,而不必等 ...

  4. 简单了解HTTP协议的基本知识,请求流程、请求方法等

    HTTP 是Hyper Text Transfer Protocol(超文本传输协议)的缩写 1.超文本传输协议是一种详细规定了浏览器和万维网服务器之间互相通信的规则. 2.HTTP协议(HyperT ...

  5. hdu 6046 hash

    题: OwO http://acm.hdu.edu.cn/showproblem.php?pid=6046 (2017 Multi-University Training Contest - Team ...

  6. Servlet中的请求转发RequestDispatcher接口的forword与Include的区别

    RequestDispatcher接口中具有两个方法: forward() 与 include() 均 可完成请求 的转发.区别如下: forword(): 使用该方法,则当前 的 Servlet 中 ...

  7. Mockito 2 参数匹配器

    Mockito 通过使用 equals() 这种自然的 Java 样式来校验参数值.有时候,当需要有其他一些灵活性的时候,你可能会要求使用参数匹配(argument matchers). 请参考下面的 ...

  8. hdu 5831 Rikka with Parenthesis II 括号匹配+交换

    Rikka with Parenthesis II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  9. poj 3190 贪心+优先队列优化

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 153 ...

  10. SDOI2019R2游记

    Day 0 上午到了济南,住在了山下.下午颓颓颓,zhy在玩炉石,我在...打元气!我的机器人终于不掉HP通关了呢,送的皮肤好好看啊. Day 1 到考场后,打开题面,一看第一题似乎很可做啊,好像可以 ...