Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read properties by dynamic keys in spring boot

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 application.properties and can be read using @Value("propertyKey") But my keys are going to be dynamic.

I know about @PropertySource to read property values and I can construct my keys dynamically. So is there any way that is provided by Spring Boot?

like image 762
rishi Avatar asked Aug 31 '16 04:08

rishi


2 Answers

you can use:

@Autowired
private Environment env;

and then load property from code:

env.getProperty("your.property")
like image 191
hi_my_name_is Avatar answered Sep 29 '22 00:09

hi_my_name_is


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"
})
like image 34
Neeraj Gahlawat Avatar answered Sep 29 '22 00:09

Neeraj Gahlawat