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

  • 相关阅读:
    JavaScript 总结
    Linux脚本shell字符串处理
    linux bash tutorial
    SVG 学习<八> SVG的路径——path(2)贝塞尔曲线命令、光滑贝塞尔曲线命令
    SVG 学习<七> SVG的路径——path(1)直线命令、弧线命令
    SVG 学习<六> SVG的transform
    SVG 学习<五> SVG动画
    SVG 学习<四> 基础API
    SVG 学习<三>渐变
    SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组
  • 原文地址:https://www.cnblogs.com/javalinux/p/14927637.html
Copyright © 2011-2022 走看看