zoukankan      html  css  js  c++  java
  • Spring boot的开启与自动关闭(主要讲‘关闭’)

    遇过的坑给你们分享一下,请略过删除线部分。
    首先:mvn依赖
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    上代码:
    @SpringBootApplication
    public class Application {

    public static ConfigurableApplicationContext context;

    public static void main(String[] args) {
    context = SpringApplication.run(Application.class, args);
    }
    }
    context中有关闭Tomcat的方法,名字叫close,在其他方法中需要关闭的时候调用就行了;

    Application.context.close();
    这篇文章写得不错:https://blog.csdn.net/jiewolf/article/details/78476537#commentsedit

    注意:想要简单的方法,看看评论!

    以上经过再次debug跟踪发现context的值为null,不是代码停止而是系统报错停止的;
    下面新提出个人方案:其实在运行spring boot时,ConfigurableApplicationContext已将被初始化好了,可以直接使用;

    @SpringBootApplication
    public class Application {

    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }
    }


    // 在需要停止的类注入
    @Autowired
    private ConfigurableApplicationContext context;

    // 然后方法中调用
    context.close();
    已测试过,其实很简单, 哎被坑了一把!!!!!!


    ————————————————
    版权声明:本文为CSDN博主「怎么可能-怎么可能」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/u014259503/article/details/82384470

  • 相关阅读:
    Azure HPC Pack Cluster添加辅助节点
    Azure HPC Pack 辅助节点模板配置
    Azure HPC Pack配置管理系列(PART6)
    Windows HPC Pack 2012 R2配置
    Azure HPC Pack 节点提升成域控制器
    Azure HPC Pack VM 节点创建和配置
    Azure HPC Pack 部署必要条件准备
    Azure HPC Pack 基础拓扑概述
    Azure VM 性能计数器配置
    Maven私仓配置
  • 原文地址:https://www.cnblogs.com/javalinux/p/14927637.html
Copyright © 2011-2022 走看看