M. Justin
M. Justin

Reputation: 21113

What do the @EnableConfigurationProperties Javadocs mean regarding "standard Spring Beans"?

The Javadocs for the value element of @EnableConfigurationProperties states:

Convenient way to quickly register @ConfigurationProperties annotated beans with Spring. Standard Spring Beans will also be scanned regardless of this value.

I am confused by this sentence: "Standard Spring Beans will also be scanned regardless of this value." I'm not sure what a "standard Spring Bean" is in this context, and how it differs from a "@ConfigurationProperties annotated bean". It sounds like there's a certain class of bean which will always be scanned for configuration properties, and certain class that won't.

To the best of my understanding, a non-@ConfigurationProperties won't be affected by the presence or absence of @EnableConfigurationProperties in the application config. It's not completely clear to me from reading the Spring Boot reference documentation either.

So in short, what does the value element of the annotation do? Why is there the need to differentiate between "standard Spring Beans" and "@ConfigurationProperties annotated beans"?

Upvotes: 1

Views: 1545

Answers (1)

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 18929

Because @ConfigurationProperties is not going to register a spring bean.

So if you had

@ConfigurationProperties
public class MyCustomProperties { 
  ...
}

Spring would not create a bean for MyCustomProperties.class

But if you had

@EnableConfigurationProperties(value = MyCustomProperties.class)
public class MyConfiguration{
  ...
}

then MyCustomProperties would be created as a spring bean

According to documentation

public @interface ConfigurationProperties Annotation for externalized configuration. Add this to a class definition or a @Bean method in a @Configuration class if you want to bind and validate some external Properties (e.g. from a .properties file).

So the annotation @ConfigurationProperties is designed to be used with some other annotations that invoke the creation of a spring bean so that it validates the properties being read.

@EnableConfigurationProperties is just an extra case where it tells Spring that some classes that bring the @ConfigurationProperties should be used to create spring beans even if no other annotation is used on those classes like @Configuration or @Component (ex MyCustomProperties.class).

Upvotes: 2

Related Questions