user1491636 user1491636 - 1 year ago 81
Java Question

Spring Boot - making configuration final

My Spring Boot (1.3.5) application uses externalized configuration using an
file. Alongside, I currently have a configuration class as such:

MyConfig {
private String prop;

// getters

I'm wondering if there is a way to make
final. Ideally I'd like to access my configuration properties publicly; for example
. I'd like to make all properties public and final; however I believe configuration classes are first instantiated via an empty constructor before properties are loaded. Is there an alternative approach?


You can inject your config values in the constructor and assign to a final field.

class MyConfig {
  private String final prop;
  public MyConfig(@Value("${prop}") String prop){
    this.prop = prop;