Spring @Configuration annotation helps in Spring annotation based configuration. @Configuration annotation indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
3.2. @EnableAutoConfiguration. The @EnableAutoConfiguration annotation enables Spring Boot to auto-configure the application context. Therefore, it automatically creates and registers beans based on both the included jar files in the classpath and the beans defined by us.
Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.
Spring Boot favors Java-based configuration. Although it is possible to use SpringApplication with XML sources, we generally recommend that your primary source be a single @Configuration class. Usually the class that defines the main method is a good candidate as the primary @Configuration .
@Configuration
It is possible to migrate the xml to a @Configuration
in a few steps:
Create a @Configuration
annotated class:
@Configuration
public class MyApplicationContext {
}
For each <bean>
tag create a method annotated with @Bean
:
@Configuration
public class MyApplicationContext {
@Bean(name = "someBean")
public SomeClass getSomeClass() {
return new SomeClassImpl(someInterestingProperty); // We still need to inject someInterestingProperty
}
@Bean(name = "anotherBean")
public AnotherClass getAnotherClass() {
return new AnotherClassImpl(getSomeClass(), beanFromSomewhereElse); // We still need to inject beanFromSomewhereElse
}
}
In order to import beanFromSomewhereElse
we need to import it's definition. It can be defined in an XML and the we'll use @ImportResource
:
@ImportResource("another-application-context.xml")
@Configuration
public class MyApplicationContext {
...
}
If the bean is defined in another @Configuration
class we can use the @Import
annotation:
@Import(OtherConfiguration.class)
@Configuration
public class MyApplicationContext {
...
}
After we imported other XMLs or @Configuration
classes, we can use the beans they declare in our context by declaring a private member to the @Configuration
class as follows:
@Autowired
@Qualifier(value = "beanFromSomewhereElse")
private final StrangeBean beanFromSomewhereElse;
Or use it directly as parameter in the method which defines the bean that depends on this beanFromSomewhereElse
using @Qualifier
as follows:
@Bean(name = "anotherBean")
public AnotherClass getAnotherClass(@Qualifier (value = "beanFromSomewhereElse") final StrangeBean beanFromSomewhereElse) {
return new AnotherClassImpl(getSomeClass(), beanFromSomewhereElse);
}
Importing properties is very similar to importing bean from another xml or @Configuration
class. Instead of using @Qualifier
we'll use @Value
with properties as follows:
@Autowired
@Value("${some.interesting.property}")
private final String someInterestingProperty;
This can be used with SpEL expressions as well.
In order to allow spring to treat such classes as beans containers we need to mark this in our main xml by putting this tag in the context:
<context:annotation-config/>
You can now import @Configuration
classes exactly the same as you would create a simple bean:
<bean class="some.package.MyApplicationContext"/>
There are ways to avoid spring XMLs altogether but they are not in the scope of this answer. You can find out one of these options in my blog post on which I'm basing my answer.
Basically I find this method of declaring beans much more comfortable than using XMLs due to a few advantages I see:
@Configuration
classes are compiled and typos just won't allow compilationsThe disadvantages are not many as I see them but there are a few which I could think of:
@Configuration
classes you must have the classes available at compile time. Usually that's not an issue, but there are cases it may be.Bottom line: It is perfectly fine to combine XMLs, @Configuration
and annotations in your application context. Spring doesn't care about the method a bean was declared with.
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