spring.profiles.active配置解析、

1、在配置springcloud 时候、不了解spring.profiles.active 有什么作用、看看文档去了只有、

查看官方文档:
2.1 Environment Repository:
http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html#_encryption_and_decryption

1

2.1 Environment Repository
Where should you store the configuration data for the Config Server? The strategy that governs this behaviour is the EnvironmentRepository, serving Environment objects. This Environment is a shallow copy of the domain from the Spring Environment (including propertySources as the main feature). The Environment resources are parametrized by three variables:

{application}, which maps to spring.application.name on the client side.
{profile}, which maps to spring.profiles.active on the client (comma-separated list).
{label}, which is a server side feature labelling a “versioned” set of config files.
Repository implementations generally behave like a Spring Boot application, loading configuration files from a spring.config.name equal to the {application} parameter, and spring.profiles.active equal to the {profiles} parameter. Precedence rules for profiles are also the same as in a regular Spring Boot application: Active profiles take precedence over defaults, and, if there are multiple profiles, the last one wins (similar to adding entries to a Map).

The following sample client application has this bootstrap configuration:

bootstrap.yml.

spring:
  application:
    name: foo
  profiles:
    active: dev,mysql

(As usual with a Spring Boot application, these properties could also be set by environment variables or command line arguments).

If the repository is file-based, the server creates an Environment from application.yml (shared between all clients) and foo.yml (with foo.yml taking precedence). If the YAML files have documents inside them that point to Spring profiles, those are applied with higher precedence (in order of the profiles listed). If there are profile-specific YAML (or properties) files, these are also applied with higher precedence than the defaults. Higher precedence translates to a PropertySource listed earlier in the Environment. (These same rules apply in a standalone Spring Boot application.)

You can set spring.cloud.config.server.accept-empty to false so that Server would return a HTTP 404 status, if the application is not found.By default, this flag is set to true.


根据官方文档我们知道以下几点:

spring.profiles.active:dev

1、dev、pro、test分别为:开发、生产、测试环境配置
2、我们除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配置文件加载配置;

2、多环境配置:

1

Spring Cloud Config also includes support for more complex requirements with pattern matching on the application and profile name. The pattern format is a comma-separated list of {application}/{profile} names with wildcards (note that a pattern beginning with a wildcard may need to be quoted), as shown in the following example:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo

If {application}/{profile} does not match any of the patterns, it uses the default URI defined under spring.cloud.config.server.git.uri. In the above example, for the “simple” repository, the pattern is simple/* (it only matches one application named simple in all profiles). The “local” repository matches all application names beginning with local in all profiles (the /* suffix is added automatically to any pattern that does not have a profile matcher).

使用YAML数组方式配置如下:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            development:
              pattern:
                - '*/development'
                - '*/staging'
              uri: https://github.com/development/config-repo
            staging:
              pattern:
                - '*/qa'
                - '*/production'
              uri: https://github.com/staging/config-repo

3、@Profile的使用

  • 使用范围:@Configration 和 @Component 注解的类及其方法,其中包括继承了@Component的注解:@Service、@Controller、@Repository等…
  • 可接受一个或者多个参数:
/**
 * @auther SyntacticSugar
 * @data 2018/12/5 0005上午 10:40
 */
@Profile("{dev},{dev1}")
@Component
public class DemoConfig {
    @Bean
    public DemoConfigOne profile1(){
        return new DemoConfigOne("dev");
    }
    @Profile("dev1")
    @Bean
    public DemoConfigTwo profile2(){
        return new DemoConfigTwo("dev1");
    }
    @Profile("dev2")
    @Bean
    public DemoConfigThr profile3(){
        return new DemoConfigThr("dev2");
    }
}

当 spring.profiles.active=dev,dev1 时,该配置类生效,且第一个和第二个@Bean生效
如果spring.profiles.active=dev,则该配置文件生效,第一个@Bean生效
如果spring.profiles.active=dev1,该配置文件未生效,所以下面的@Bean都不会生效


推荐原文:https://blog.csdn.net/swordsnapliu/article/details/78540902
以及:http://www.leftso.com/blog/111.html
原文有些许出和官方文档不一致处、酌情参考;

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]和\[2\]中的错误信息显示,出现了"Could not resolve placeholder"的错误,这意味着在配置文件中找不到对应的占位符。具体来说,错误信息中提到了'spring.datasource.druid.initialSize'和'show.tips.text'这两个占位符无法解析。引用\[3\]中给出了一些解决方法。首先,确认配置文件中是否存在这些字段,并且字段名是否正确。其次,检查配置文件是否正确引用了相应的配置文件。在Spring Boot中,我们可以使用application.yml文件来指定使用哪个配置文件。例如,可以在application.yml中使用'spring.profiles.active'来指定使用名为'druid'的配置文件。如果以上步骤都正确无误,但仍然出现该错误,可能是由于其他原因导致的。可以进一步检查项目的依赖和版本是否正确,以及相关的配置是否正确。 #### 引用[.reference_title] - *1* *3* [springboot启动报错Could not resolve placeholder xxx](https://blog.csdn.net/a_helloword/article/details/118334203)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【解决方法汇总】SpringBoot项目报错 Could not resolve placeholder ‘‘ in value “${}“](https://blog.csdn.net/yu1812531/article/details/123466616)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值