Read properties by dynamic keys in spring boot

前端 未结 2 799
南旧
南旧 2020-12-09 19:24

I wanted to know if there is any way in Spring Boot to read property values from properties file by using Dynamic Keys. I know properties can be put in applic

相关标签:
2条回答
  • 2020-12-09 20:13

    you can use:

    @Autowired
    private Environment env;
    

    and then load property from code:

    env.getProperty("your.property")
    
    0 讨论(0)
  • 2020-12-09 20:28

    1- Register a Properties File via Java Annotations.

    @Configuration
    @PropertySource("classpath:test.properties")
    public class PropertiesJavaConfig {
        
    }
    

    2- Dynamically select the right file at runtime.

    @PropertySource({ 
      "classpath:persistence-${envTarget:DB}.properties"
    })
    
    0 讨论(0)
提交回复
热议问题