0

Is it possible to create a ConfigurationProperties bean in runtime after the spring context is ready?
Otherwise, is it possible to generate a ConfigurationProperties class at runtime (instrumentation) which will be collected to a ConfigurationProperties by spring in the post-process phase?

As I noticed, ConfigurationPropertiesBinder.class was public at the 2nd spring version.
However turned private recently. This class provided api to create and bind a ConfigurationPropertiesBean.

Thanks,

Ami
  • 185
  • 1
  • 8
  • Could you describe the exact use case for such functionality? – crizzis Apr 11 '21 at 08:31
  • @crizzis ConfigurationProperties is a great annotation which allows simple binding to properties in hierarchical manner. However, I found my self adding wrapper POJO classes just to maintain certain properties naming convention. For example, I have MainConfig(prefix=com.company.service.conf) which holds the member names(List). thus it will be binded to com.company.service.conf.names[0...n] properties. I would like it to be binded to com.company.service.conf.dns.names[0...n] instead. Currently I'll have to create a wrapper class to names(List) and call its instance dns to achiev – Ami Apr 12 '21 at 09:16
  • while I would like to avoid this class creation in case of a withered wrapper class. Such that I can for example, decorate the 'names' member with MyConfigurationPropertiesExtender(suffix="dns.names") annotation to achieve this naming instead of maneuvering with wrapper classes – Ami Apr 12 '21 at 10:18
  • Not sure I follow. How is this related to creating `ConfigurationProperties` beans at runtime? The whole idea of `@ConfigurationProperties` is to provide compile-time safety when injecting properties to other beans. Also, if you want to skip the `getDns()` part, you can always use a logical property – crizzis Apr 12 '21 at 16:09
  • @crizzis what do you mean by logical property? all members that will autowire these configuration beans will autowire it in lazy manner. such that if the configuration bean that is registered in runtime isn't yet ready - it won't stop the initialization of the other class that have this configuration bean injected to. – Ami Apr 12 '21 at 20:01
  • I meant that you can create methods like `getNames()` which will call `getDns().getNames()` internally. You can also make `getDns()` non-public – crizzis Apr 13 '21 at 16:22

0 Answers0