• SpringBoot 加载外部配置文件 不重启服务 自动更新外部配置


    常规启动类:

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

    加载外部配置文件启动类:

    工具类

    package cn.com.aia.robot.util;
    
    import cn.com.aia.robot.ReleaseRobotsApplication;
    import org.springframework.boot.SpringApplication;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PropertiesLoaderUtils;
    
    public class ApplicationContextUtils {
    
        private static String[] args;
        private static ApplicationContext context;
    
        private ApplicationContextUtils() {
        }
    
        public static void init(ApplicationContext applicationContext, String[] args) {
            ApplicationContextUtils.context = applicationContext;
            ApplicationContextUtils.args = args;
        }
    
        public static void setApplicationContext(ApplicationContext applicationContext) {
            ApplicationContextUtils.context = applicationContext;
        }
    
        public static void setArgs(String[] args) {
            ApplicationContextUtils.args = args;
        }
    
        /**
         * 刷新ConfigurableApplicationContext,实现不重启服务,自动更新配置
         * Resource resource指向外部配置文件
         */
        public static void restart(Resource resource) throws Exception {
            if(context instanceof ConfigurableApplicationContext) {
                ((ConfigurableApplicationContext) context).close();
                SpringApplication app = new SpringApplication(ReleaseRobotsApplication.class);
                app.setDefaultProperties(PropertiesLoaderUtils.loadProperties(resource));
                ConfigurableApplicationContext applicationContext = app.run(args);
                ApplicationContextUtils.setApplicationContext(applicationContext);
            }
        }
    
        public static <T> T getBean(String beanName) {
            return (T) context.getBean(beanName);
        }
    
        public static <T> T getBean(Class<T> t) {
            return context.getBean(t);
        }
    }

    启动类

    @SpringBootApplication
    public class MyApplication {
    
        public static void main(String[] args) throws Exception {
            SpringApplication app = new SpringApplication(MyApplication.class);
            app.setDefaultProperties(PropertiesLoaderUtils.loadProperties(new PathResource(Paths.get("C:/xxx/xxx.properties"))));
            //app.setDefaultProperties(PropertiesLoaderUtils.loadProperties(new UrlResource("http://xxx/xxx.properties")));
            ConfigurableApplicationContext applicationContext = app.run(args);
            ApplicationContextUtils.init(applicationContext, args);
        }
    }
  • 相关阅读:
    Windows API的CreateFile()中的OPEN_ALWAYS和CREATE_ALWAYS之间的差异
    Linux下的虚拟串口对(可用于在本机上模拟串口进行调试)
    [转&精]IO_STACK_LOCATION与IRP的一点笔记
    【转】在WIN7、WIN10操作系统用WebDAV映射网络驱动器需要的操作
    Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information
    分布式处理大数据的目录及学习树
    利用docker将传统企业集中式架构转换为微服务架构的实践经验
    docker学习笔记5 端口映射与容器互联
    docker学习笔记4 数据管理、持久化
    docker学习笔记3 容器 container
  • 原文地址:https://www.cnblogs.com/bevis-byf/p/14373850.html
Copyright © 2020-2023  润新知