简体   繁体   English

Spring 引导:如何从 application.yml 中读取特定属性并忽略其他属性

[英]Spring boot: how to read specific property from application.yml and ignore others

My goal is to read some property from application.yml file and ignore other properties.我的目标是从 application.yml 文件中读取一些属性并忽略其他属性。 This is need for testing purposes - I want to read part of config and validate it.这是出于测试目的——我想阅读部分配置并验证它。 I want to ignore other properties because Spring Boot tring to handle it but this is no need for my test.我想忽略其他属性,因为 Spring Boot tring 可以处理它,但这对我的测试来说不需要。 Example.例子。

Config:配置:

first:
 property:
  {cipher}value    #for example Spring trying to handle cipher and throws exception

second:
 property:
  value

Class ( using String for simplification ): Class(使用 String 进行简化):


@Configuration
public class Configuration {

  @Bean
  @ConfigurationProperties(prefix = "second.property")
  public String secondProperty() {
      return new String();
  }
}

Test class:测试class:

@SpringBootTest
public class ConfigPropertiesCheckerIntegrationTest {

    @Autowired
    String secondProperty;

    @Test
    void checkAutoConfigEndpointProperties() {
        assertThat(secondProperty, is(notNullValue()));
    }
}

So question: how to ignore first.property and say Spring just skip it?所以问题:如何忽略first.property并说 Spring 只是跳过它?

You can disable {cipher} ed properties decryption with spring.cloud.decrypt-environment-post-processor.enabled=false .您可以使用spring.cloud.decrypt-environment-post-processor.enabled=false禁用{cipher} ed 属性解密。 Eg:例如:

@SpringBootTest(properties = "spring.cloud.decrypt-environment-post-processor.enabled=false")

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

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