• 自定义springboot的生命周期函数在项目启动完成后去取配置文件中的值


    主要是实现smartLifecycle类

    package com.ruoyi.workflow.util;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.SmartLifecycle;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyShutdownHook  implements SmartLifecycle {
         @Autowired
        private ApplicationContext applicationContext;
        private boolean isRunning = false;
    
        /**
         * 1. 我们主要在该方法中启动任务或者其他异步服务,比如开启MQ接收消息
    * 2. 当上下文被刷新(所有对象已被实例化和初始化之后)时,将调用该方法,默认生命周期处理器将检查每个SmartLifecycle对象的isAutoStartup()方法返回的布尔值。 * 如果为“true”,则该方法会被调用,而不是等待显式调用自己的start()方法。 */
    @Override public void start() { System.out.println("start"); // 获取配置文件中的值 String configValue =applicationContext.getEnvironment().getProperty("name"); System.err.println("开始了完美的一天"+configValue); // 执行完其他业务后,可以修改 isRunning = true isRunning = true; } /** * 如果工程中有多个实现接口SmartLifecycle的类,则这些类的start的执行顺序按getPhase方法返回值从小到大执行。
    * 例如:1比2先执行,-1比0先执行。 stop方法的执行顺序则相反,getPhase返回值较大类的stop方法先被调用,小的后被调用。 */
    @Override public int getPhase() { // 默认为0 return 0; } /** * 根据该方法的返回值决定是否执行start方法。
    * 返回true时start方法会被自动执行,返回false则不会。 */
    @Override public boolean isAutoStartup() { // 默认为false return true; } /** * 1. 只有该方法返回false时,start方法才会被执行。
    * 2. 只有该方法返回true时,stop(Runnable callback)或stop()方法才会被执行。 */
    @Override public boolean isRunning() { // 默认返回false return isRunning; } /** * SmartLifecycle子类的才有的方法,当isRunning方法返回true时,该方法才会被调用。 */ @Override public void stop(Runnable callback) { System.out.println("stop(Runnable)"); // 如果你让isRunning返回true,需要执行stop这个方法,那么就不要忘记调用callback.run()。 // 否则在你程序退出时,Spring的DefaultLifecycleProcessor会认为你这个TestSmartLifecycle没有stop完成,程序会一直卡着结束不了,等待一定时间(默认超时时间30秒)后才会自动结束。 // PS:如果你想修改这个默认超时时间,可以按下面思路做,当然下面代码是springmvc配置文件形式的参考,在SpringBoot中自然不是配置xml来完成,这里只是提供一种思路。 // // // // callback.run(); isRunning = false; } /** * 接口Lifecycle的子类的方法,只有非SmartLifecycle的子类才会执行该方法。
    * 1. 该方法只对直接实现接口Lifecycle的类才起作用,对实现SmartLifecycle接口的类无效。
    * 2. 方法stop()和方法stop(Runnable callback)的区别只在于,后者是SmartLifecycle子类的专属。 */
    @Override public void stop() { System.out.println("stop"); isRunning = false; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
  • 相关阅读:
    vue脚手架(vue-cli)详细安装过程
    (Java实现) HDOJ 1051 Wooden Sticks 贪心算法
    UE5.1 + Android 环境搭建
    bond0双网卡主备实验
    系统集成项目管理工程师有什么用?
    mac 和 windows 相互传输文件【共享文件夹】
    【Python 实战】---- 使用 RemoveBg 实现一键批量抠图
    脚本语言Lua的基础知识总结
    解决【无法处理文件xxx,因为它位于 Internet 或受限区域中,或者文件上具有 Web 标记。要想处理这些文件,请删除 Web 标记】问题
    NewStarCTF2023week2-R!!C!!E!!
  • 原文地址:https://blog.csdn.net/weixin_47615289/article/details/134474008