zoukankan      html  css  js  c++  java
  • 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置

    https://blog.csdn.net/swordsnapliu/article/details/78540902

    引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发、测试、生产)配置参数的切换

    一.根据springboot的配置文件命名约定,结合active可在不同环境引用不同的properties外部配置

    参考官方文档:
    springboot官方文档截图
    根据文档描述,我们除application.properties外,还可以根据命名约定(
    命名格式:application-{profile}.properties)来配置,如果active赋予的参数没有与使用该命名约定格式文件相匹配的话,app则会默认从名为application-default.properties 的配置文件加载配置。
    如:spring.profiles.active=hello-world,sender,dev 有三个参数,其中 dev 正好匹配下面配置中的application-dev.properties 配置文件,所以app启动时,项目会先从application-dev.properties加载配置,再从application.properties配置文件加载配置,如果有重复的配置,则会以application.properties的配置为准。(配置文件加载顺序详见官方文档:24. Externalized Configuration)
    springboot外部配置
    如此,我们就不用为了不同的运行环境而去更改大量的环境配置了(此处,dev、pro、test分别为:开发、生产、测试环境配置)

    二.通过@Profile注解匹配active参数,动态加载内部配置
    参考官方文档:
    springboot官方文档截图

    1.@Profile注解使用范围:@Configration 和 @Component 注解的类及其方法,其中包括继承了@Component的注解:@Service、@Controller、@Repository等…
    2.@Profile可接受一个或者多个参数,例如:

    @Profile({"tut1","hello-world"})
    @Configuration
    public class Tut1Config {
        @Bean
        publicQueuehello() {
            return newQueue("hello");
        }
        @Profile("receiver")
        @Bean
        publicTut1Receiverreceiver() {
            return newTut1Receiver();
        }
        @Profile("sender")
        @Bean
        publicTut1Sendersender() {
            return newTut1Sender();
        }
    }

    当 spring.profiles.active=hello-world,sender 时,该配置类生效,且第一个@Bean和第三个@Bean生效
    如果spring.profiles.active=hello-world ,则该配置文件生效,第一个@Bean生效
    如果spring.profiles.active=sender ,该配置文件未生效,所以下面的@Bean都不会生效
    如此,当我们的项目需要运行在不同环境,特异化配置又比较多,该注解的优势是相当明显的!

    三.@Profile 注解取反
    请看下面代码:

    @Component
    public class Tut1CommindLineRunner {
        @Profile("usage_message")
        @Bean
        publicCommandLineRunnerusage() {
            return strings -> {
                System.out.println("This app uses Spring Profiles to control its behavior.
    ");
                System.out.println("Sample usage: java -jar rabbit-tutorials.jar --spring.profiles.active=hello-world,sender");
            };
        }
        @Profile("!usage_message")
        @Bean
        publicCommandLineRunnertutorial() {
            return newRabbitAmqpTutorialsRunner();
        }
    }

    该@Profile注解接收了两个类似于取反一样的配置参数,该用法我没有找到相关的官方文档,这是在rabbitmq官网,关于springboot amqp 的一段代码。
    通过查看源代码,我发现,其实这个就是一个类似于取反的意思,这里理解为:匹配不上该配置参数的时候,启用该配置,相关源代码:

    package org.springframework.core.env;
    //...
    public abstract classAbstractEnvironmentimplementsConfigurableEnvironment {
        //...
        public booleanacceptsProfiles(String... profiles) {
            Assert.notEmpty(profiles,"Must specify at least one profile");
            String[] var2 = profiles;
            int var3 = profiles.length;
            for(intvar4 =0;var4 < var3;++var4) {
                String profile = var2[var4];
                if(StringUtils.hasLength(profile) && profile.charAt(0) == 33) {
                    if(!this.isProfileActive(profile.substring(1))) {
                        return true;
                    }
                } else if(this.isProfileActive(profile)) {
                    return true;
                }
            }
            return false;
        }
    
        protected booleanisProfileActive(String profile) {
            this.validateProfile(profile);
            Set<String> currentActiveProfiles =this.doGetActiveProfiles();
            return currentActiveProfiles.contains(profile) || currentActiveProfiles.isEmpty() &&this.doGetDefaultProfiles().contains(profile);
        }
        //...
    }

    “!”对应的char 编码为:33
    此处:StringUtils.hasLength(profile) && profile.charAt(0) == 33 ,如果配置以 “!”开头,则将“!”后的字符串与配置进行匹配,并取反,如果为true,则返回true,即active没有配置该配置参数,则返回true,即启动该配置。

    如有理解错误或者描述不正确的地方,欢迎留言!

    参考:
    http://blog.csdn.net/he90227/article/details/52981747 不错的博客
    https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/htmlsingle/ 官方文档

  • 相关阅读:
    avaya电话重置
    Zscaler Client Connector
    tcpdump port 514
    rsyslog和过滤规则
    syslog,rsyslog和syslog-ng
    Ubuntu 搭建Rsyslog服务器
    syslog日志的类型和级别
    springboot_springSecurity整合
    springboot_整合JDBC_Druid数据源_MyBatis
    springboot_数据增删改查
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/9710222.html
Copyright © 2011-2022 走看看