美文网首页
springboot 配置文件外置

springboot 配置文件外置

作者: 泥煤的嘎嘎 | 来源:发表于2020-05-11 18:08 被阅读0次

    SpringApplicationRunListener减少

    package com.hengtiansoft.dscs.gateway.listener;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.SpringApplicationRunListener;
    import org.springframework.boot.system.ApplicationHome;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.core.Ordered;
    import org.springframework.core.env.ConfigurableEnvironment;
    import org.springframework.core.env.MutablePropertySources;
    import org.springframework.core.env.PropertiesPropertySource;
    import org.springframework.core.env.PropertySource;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Properties;
    
    /**
     * 加载外部配置文件监听器
     *
     * @author leiwu
     */
    @Slf4j
    public class LoadExternalPropertiesApplicationRunnerListener implements SpringApplicationRunListener, Ordered {
    
        private SpringApplication application;
        private String[] args;
    
        private static final String PROPERTIES_NAME = "env-var.properties";
    
        public LoadExternalPropertiesApplicationRunnerListener(SpringApplication application, String[] args) {
            this.application = application;
            this.args = args;
        }
    
        @Override
        public void starting() {
            log.info("load external properties start...");
        }
    
        @Override
        public void environmentPrepared(ConfigurableEnvironment environment) {
            // 配置文件读取到程序中 思路需要自己将本地文件读取到程序中,让后在放入到SpringBoot容器
    
            // 1 获取执行jar路径
            ApplicationHome h = new ApplicationHome(getClass());
            File jar = h.getSource();
            String executeJarPath = jar.getParentFile().toString() + "/";
            System.out.println("jar包执行路径为: " + executeJarPath);
    
            // 2 加载配置文件
            Properties properties = new Properties();
            try {
                // 判断文件是否存在
                File file = new File(executeJarPath + PROPERTIES_NAME);
                if (file.exists()) {
                    // 1  读取properties文件
                    BufferedReader bufferedReader = new BufferedReader(new FileReader(executeJarPath + PROPERTIES_NAME));
                    properties.load(bufferedReader);
                    // 2  读取名称名称为env-var
                    PropertySource propertySource = new
                            PropertiesPropertySource("env-var", properties);
                    // 3  将资源添加到SprigBoot项目中
                    MutablePropertySources propertySources = environment.getPropertySources();
                    // 4  增加到SpringBoot项目中
                    propertySources.addLast(propertySource);
    
                    System.out.println("成功加载外部配置文件: " + PROPERTIES_NAME);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void contextPrepared(ConfigurableApplicationContext context) {
    
        }
    
        @Override
        public void contextLoaded(ConfigurableApplicationContext context) {
    
        }
    
        @Override
        public void started(ConfigurableApplicationContext context) {
    
        }
    
        @Override
        public void running(ConfigurableApplicationContext context) {
    
        }
    
        @Override
        public void failed(ConfigurableApplicationContext context, Throwable exception) {
    
        }
    
        @Override
        public int getOrder() {
            return -1;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:springboot 配置文件外置

          本文链接:https://www.haomeiwen.com/subject/fchpnhtx.html