Skip to content

How to access configuration beans in Spring controllers

I have a Spring Boot application that uses Spring profiles to create environment specific configurations, for example:

public class LocalConfiguration

public class ProdConfiguration

I have a @RestContoller that needs to access the values that the configurations load from How can I inject the current environment specific configuration bean inside the controller?


private <config_based_on_env_here> config;

public String getSomeString() {
    return config.getSomeString();



If you need to access values from the or .yaml configuration you could use a much simpler way for achieving this.

Firstly configure different configs:



Create a configuration for reading needed value:

@ConfigurationProperties(prefix = "my")
public class ConfigProperties {
    private String value;

    // standard getters and setters

Finally, you could autowire this configuration at the controller:

class MyController {

    private ConfigProperties config;

    public void hello() {
      System.out.println("Config value: " + config.getValue()); 

When you run your app with the needed profile – the appropriate config file will be loaded and accessed at the controller.

Also, you could have a look at @Value annotation

Additional resources:

9 People found this is helpful