springboot 配置文件外置

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;
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。