Configuring beans using Java
Before Spring 3, you could only define beans using XML. Spring 3 introduced the @Configuration
, @Bean
, @import
, and @DependsOn
annotations to configure and define Spring beans using Java.
You have already learned about the @Configuration
and @Bean
annotations in the Defining a bean and its scope section. Now, you will explore how to use the @Import
and @
DependsOn
annotations.
The @Import
annotation is more useful when you develop an application without using autoconfiguration.
The @Import annotation
@Import
is used for modularizing configurations when you have more than one configuration class. You can import the bean’s definitions from other configuration classes, and this is useful when you instantiate the context manually. Spring Boot uses auto-configuration, so you don’t need to use @Import
. However, you would have to use @Import
to modularize the configurations if you want to instantiate the context manually.
Let’...