Read properties by dynamic keys in spring boot

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"
})

In case you are reading from the application.properties, you just define the environment spring autowired variable as specified by freakman (org.springframework.core.env.Environment). But if you are using a new properties file specific for certain properties, you can use the following code:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

 @Configuration
 @PropertySource("classpath:filename.properties")
 public class CustomConfig {
    @Autowired
    Environment env;


    public String getProperty(String keyName) {
       return env.getProperty(keyName);
    }    
 }

you can use:

@Autowired
private Environment env;

and then load property from code:

env.getProperty("your.property")