@ConfigurationProperties do not seem to work when also injecting a bean

39
June 22, 2022, at 11:50 PM

I have the following situation:

@Configuration
@ConfigurationProperties(prefix = "my.prefix")
@ConditionalOnProperty(value = "my.prefix.should-enable", havingValue = "true")
@RequiredArgsConstructor // lombok
public class MyConf {

  @Setter
  private Map<String, SettingOverride> overrides = Collections.emptyMap();
  private final RestTemplate restTemplate;
  @Data
  public static class SettingOverride {
  
    private final boolean enabled;
  }
}

And the following configuration (enabled only for a specific profile via application-profilename.yml):

---
my:
  prefix:
    should-enable: true
    overrides:
      SOME_SETTING:
        enabled: true

The RestTemplate bean is successfully injected, yet the my overrides Map is always empty. A few days ago when I last tested this code, this approach seemed to work.

Interestingly, I found this bit in the reference documentation:

We recommend that @ConfigurationProperties only deal with the environment and, in particular, does not inject other beans from the context. For corner cases, setter injection can be used or any of the *Aware interfaces provided by the framework (such as EnvironmentAware if you need access to the Environment). If you still want to inject other beans using the constructor, the configuration properties bean must be annotated with @Component and use JavaBean-based property binding.

which makes me think that this approach should never have worked (I'm unable to figure out why it even worked, as alternative approaches to the above have all failed).

Trying to make my @Configuration class inject the RestTemplate via e.g. ApplicationContextAware and a default constructor, as suggested in the above documentation, does not work either.

Since this is a test configuration class, it would be ideal if all corresponding properties and structures would be in a single class.

A different configuration class which uses other properties, e.g. (note: no injected beans) :

@Data // lombok
@Configuration
@ConditionalOnProperty(value = "my.other-prefix.should-enable", havingValue = "true")
@ConfigurationProperties(prefix = "my.other-prefix")
public class MyOtherConf {
  private String someValue;
  private Map<String, String> settings = Collections.emptyMap();
}

with a config of:

---
my:
  other-prefix:
    should-enable: true
    settings:
      another-value: "anothervalue"

seems to work without issues. What am I doing wrong?

READ ALSO
Why I am unable to wrap the execution of a request with an @Around advice?

Why I am unable to wrap the execution of a request with an @Around advice?

I work on a REST API, and I'd like to do some very simplistic tests to see how long it takes to process a request

40
removeRow using XSSF libraries is not working

removeRow using XSSF libraries is not working

I tried the below code, but it's not working

71
WatchService watching files on distributed filesystem from multiple machines

WatchService watching files on distributed filesystem from multiple machines

I am trying to implement a WatchService that watches a filesystem for changesOnce files get created I want to create tasks for each new file and add that task to a distributed queue to let it get processed from multiple machines

36
Accessing local database from kubernetes cluster

Accessing local database from kubernetes cluster

I have a database running in local machine which i can access it using 1270

59