Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access @ConfigurationProperties bean by it's name from XML configuration?

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:

@ConfigurationProperties beans can be registered in the standard way (for example using @Bean methods) 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.

like image 525
Kirill Avatar asked Jan 27 '23 22:01

Kirill


1 Answers

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}

like image 127
Rentius2407 Avatar answered Jan 30 '23 01:01

Rentius2407