栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot 自定义配置文件路径

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot 自定义配置文件路径

spring boot 自定义配置文件路径
    • bootstrap.yml
    • 启动类

bootstrap.yml
#系统级别的参数配置,用于应用程序上下文的引导阶段,先于application.yml加载
spring:
  config:
    additional-location: file:/opt/appconf/
  profiles:
    active:
      - 'devd'
      - 'common'
启动类
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;


@SpringBootApplication(scanbasePackages = "com")
@EnableCaching
@EnableDiscoveryClient
public class WebApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        new SpringApplicationBuilder(WebApplication.class).properties("spring.config.name:application-additional-location", getAdditionalLocation()).build().run(args);
//			SpringApplication.run(WebApplication.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WebApplication.class).properties("spring.config.name:application-additional-location", getAdditionalLocation());
//        return builder.sources(WebApplication.class);
    }
    private static String getAdditionalLocation() {
        //		如果file: 与resource目录相同优先使用file:
        String addClassPath = "spring.config.additional-location:" +
                "file:./config/,file:./," +
                "classpath:/config/," +
                "classpath:/,";
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource inputFile = resourceLoader.getResource("classpath:bootstrap.yml");
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(inputFile);
        yamlPropertiesFactoryBean.afterPropertiesSet();
        if (yamlPropertiesFactoryBean.getObject().containsKey("spring.config.additional-location")) {
            addClassPath = addClassPath.concat(yamlPropertiesFactoryBean.getObject().get("spring.config.additional-location").toString());
            System.out.println(addClassPath);
        }
        return addClassPath;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322455.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号