Suppose I have an AppProperties POJO annotated with @ConfigurationProperties("app"). It contains different app properties which I usually use to configure my beans in @Configuration classes.
@EnableConfigurationProperties(AppProperties.class) makes it available for @Autowiring, this is very convenient for Java-configuration.
But one part of my app's context is configured using old-fashined XML-confiuration. I would like to know how can I access @ConfigurationProperties AppProperties bean in XML configuration? If only @EnableConfigurationProperties provided me an ability to give a name, I could probably use SpEL in XML like this: #{appProperties.requiredProp} (I would really like to achieve it).
Unfortunately I do not see a way to provide the name and my attempt to use suggested appProperties name fails with:
SpelEvaluationException: EL1008E: Property or field 'appProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
I've seen in debugger that the bean is actually called app-my.package.AppProperties which is not obvious and never stated explicitly.
In JavaDoc for @EnableConfigurationProperties I noticed the following:
@ConfigurationPropertiesbeans can be registered in the standard way (for example using@Beanmethods) or, for convenience, can be specified directly on this (@EnableConfigurationProperties) annotation.
Does this mean I can somehow get a named instance of AppProperties with values injected from application.properties using either @Bean method in Java or <bean/> in XML?
Of course, I could autowire the whole AppProperties to the class configured with XML, but I do not think it is a good design solution, because am only interested in a single String property from AppProperties. Probably I should not try to use this "type-safe" properties manner in XML and just stick to old way: using ${}-resolution, but I feel like I am missing something in the @ConfigurationProperties concept so please let me know.
There are two ways to register a configuration properties bean.
Registering with @EnableConfigurationProperties.
@EnableConfigurationProperties(AppProperties.class)
public class Application {}
@ConfigurationProperties("app")
public class AppProperties {}
@EnableConfigurationProperties annotation will automatically register AppProperties class as a bean called prefix-your.package.AppProperties, when you include the class as follows: @EnableConfigurationProperties(AppProperties.class) and AppProperties has got a prefix, @ConfigurationProperties("app").
Registering with @Component or @Bean
@EnableConfigurationProperties
public class Application {}
@Component
@ConfigurationProperties("app")
public class AppProperties {}
or
@EnableConfigurationProperties
public class Application {
@Bean
@ConfigurationProperties("app")
public AppProperties appProperties() {
return new AppProperties();
}
}
The @Component and @Bean will register a bean called appProperties.
By using the second approach you would be able to use SpEL to get the property #{appProperties.requiredProp}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With