简体   繁体   English

Spring Boot忽略spring.config.name/location属性

[英]Spring Boot ignores spring.config.name/location properties

I've got quite a simple application.yml file: 我有一个非常简单的application.yml文件:

spring:
  config:
    name: android,ios,test,web

在此处输入图片说明

I expected to gain an ability to name config files like android.yml and put them into 我希望能够命名诸如android.yml这样的配置文件并将其放入

classpath:/,classpath:/config/,file:./,file:./config/

as the DEFAULT_SEARCH_LOCATIONS constant from the ConfigFileApplicationListener class specifies. ConfigFileApplicationListener类中指定的DEFAULT_SEARCH_LOCATIONS常量一样。 I created a file in the same directory with the main config: 我在与主配置相同的目录中创建了一个文件:

android:
  clientId: 0
  clientSecret: clientSecret

在此处输入图片说明

Then I wrote a @Configuration class with one method to get an instance of ClientDetails by the @ConfigurationProperties : 然后,我用一种方法编写了一个@Configuration类,以通过@ConfigurationProperties获取ClientDetails的实例:

@Configuration
public class TrustedClientInformationConfiguration {

    @Bean(name = ANDROID)
    @ConfigurationProperties(prefix = ANDROID)
    public ClientDetails getAndroidClientDetails() {
        return new BaseClientDetails();
    }

}

Unfortunately, after autowiring, I got the instance with unfilled fields. 不幸的是,在自动装配之后,我得到了带有未填写字段的实例。 What have I missed? 我错过了什么?

EDIT1: I found and debugged a method where CONFIG_NAME_PROPERTY = "spring.config.name" is used (it's only one usage), the containsProperty condition always returns false : EDIT1:我发现并调试了使用CONFIG_NAME_PROPERTY = "spring.config.name"的方法(这只是一种用法), containsProperty条件始终返回false

private Set<String> getSearchNames() {
    if (this.environment.containsProperty(CONFIG_NAME_PROPERTY)) {
        return asResolvedSet(this.environment.getProperty(CONFIG_NAME_PROPERTY),
                null);
    }
    return asResolvedSet(ConfigFileApplicationListener.this.names, DEFAULT_NAMES);
}

EDIT2: It is a try to create an oauth2 client configuration by moving properties to a separate file for each trusted client. EDIT2:尝试通过将属性移动到每个受信任客户端的单独文件来创建oauth2客户端配置。 They should be always instantiated despite the active profile. 尽管配置文件处于活动状态,但应始终实例化它们。

The spring.config properties should be set on the command-line as they're needed before any config files are loaded. 应该在加载任何配置文件之前根据需要在命令行上设置spring.config属性。

However it looks like the feature you actually want is profiles . 但是,看起来您实际上想要的功能是配置文件 They're a much easier way to organise different configuration for different environments. 它们是为不同环境组织不同配置的简便方法。 Files can be named application-android , application-test , etc. 文件可以命名为application-androidapplication-test等。

Documentation 文献资料

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM