• springboot 选择启动某个配置文件


    选择启动某个配置文件

    Spring Boot配置文件提供了隔离一部分应用程序配置的方法,并可使其仅在某指定环境可用。任何有@Component和@Configuration注解的Bean都用@profile来指定加载哪个配置文件。如:

    @Configuration 
    @Profile( “production”)//加载production配置文件,即也代表当前是production环境
    public class Demo{
        // ...
    }
    

    以普通Spring的方式,可以使用spring.profile.active环境属性来指定哪些配置文件处于活动状态。

    spring.profiles.active = dev
    或
    spring: 
        profiles: 
            active: dev
    
    
    
    4.1以编程方式启动某配置文件
    
    @SpringBootApplication
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(DemoApplication.class);
            //启动dev配置文件
            app.setAdditionalProfiles("dev");   // dev 或prod
            app.run(args);
        }
    }

    4.2 maven的pom文件中启动某配置文件

    <profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <!-- 开启本地开发环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>
    </profiles>
    

    5. YAML下的列表

    如,以下yaml配置文件:

    book: 
         list: 
            -name: Java
            -name: C++

    可用以下形式获取book列表:

    @ConfigurationProperties(“book”)
     public class FooProperties{
        private final List <MyPojo> list = new ArrayList <>();
        public List <MyPojo>  getList(){
             return this .list;
        }
    }

     

  • 相关阅读:
    【阿里的感悟】质量该如何做? .(转载)
    java linux 配置环境
    Spring Bean属性绑定Bean返回值
    Spring BeanNameAutoProxyCreator 与 ProxyFactoryBean
    Spring Aop之(二)Aop 切面声明和通知
    Ubuntu开机自动启动Script
    转战博客园!
    linux 系统管理11 ——系统安全及应用
    linux awk
    Rsync数据同步工具
  • 原文地址:https://www.cnblogs.com/shihaiming/p/9350665.html
Copyright © 2020-2023  润新知