Why do you need to specify @Configuration when you want to use @PropertySource in Spring Boot?

Viewed 44

I know you use @Configuration when you want to add @Bean for ApplicationContext. But why do you need to specify @Configuration when you want to use @PropertySource in Spring Boot?

1 Answers

From the API documentaion of @PropertySource:

Annotation providing a convenient and declarative mechanism for adding a PropertySource to Spring's Environment. To be used in conjunction with @Configuration classes.

So, it can be deduced that Spring will register a org.springframework.core.env.PropertySource bean if we use @PropertySource which will be managed by the Spring container. And @Configuation is needed for declaring Spring beans.

Related