Question Details

No question body available.

Tags

java spring-boot configurationproperties

Answers (2)

Accepted Answer Available
Accepted Answer
June 22, 2025 Score: 5 Rep: 3,915 Quality: High Completeness: 60%

Spring Boot exposes org.springframework.boot.context.properties.bind.Binder, which you can use like this:

import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;

public class MyApplicationPreparedListener implements ApplicationListener {

@Override public void onApplicationEvent(ApplicationPreparedEvent event) { ConfigurableEnvironment env = event.getApplicationContext().getEnvironment(); Binder binder = new Binder(ConfigurationPropertySources.get(env));

MyComplexConfig config = binder.bind("my.config", MyComplexConfig.class) .orElseThrow(() -> new IllegalStateException("Could not bind config"));

System.out.println("Loaded config:"); System.out.println(" name: " + config.getName()); System.out.println(" servers: " + config.getServers()); System.out.println(" timeout: " + config.getNested().getTimeout()); System.out.println(" labels: " + config.getNested().getLabels()); } }

Lets take a configuration properties file and two YAMLs like this:

import lombok.Getter;
import lombok.Setter;

import java.util.List; import java.util.Map;

@Getter @Setter public class MyComplexConfig { private String name; private List servers; private NestedConfig nested;

@Getter @Setter public static class NestedConfig { private int timeout; private Map labels; } }

application.yaml

my:
  config:
    name: DefaultConfig
    servers:
      - default1.server.com
      - default2.server.com
    nested:
      timeout: 30
      labels:
        env: prod
        region: us-east

application-dev.yaml

my:
  config:
    name: DevConfig
    servers:
      - dev1.server.local
    nested:
      timeout: 10
      labels:
        env: dev
        debug: true

SpringBootApplication class:

import com.example.MyApplicationPreparedListener;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication public class ExampleApplication {

public static void main(String[] args) { new SpringApplicationBuilder(ExampleApplication.class) .profiles("dev") // remove it to run with default profile .listeners(new MyApplicationPreparedListener()) .run(args); } }

Output for default profile:

Loaded config: name: DefaultConfig servers: [default1.server.com, default2.server.com] timeout: 30 labels: {env=prod, region=us-east}

Output for dev profile:

Loaded config: name: DevConfig servers: [dev1.server.local] timeout: 10 labels: {env=dev, debug=true, region=us-east}
June 22, 2025 Score: 0 Rep: 638 Quality: Low Completeness: 40%

At that point the Environment would be ready to use, even if beans are not created yet.

So you can inject the Environment into your class, then execute the old environment.getProperty("my.property") method.