I came across two annotations provided by Spring 3 (@Component and @Configuration)
I am a bit confused between these.
Here is what I read about @Component
Put this “context:component” in the bean configuration file, it means, enable the auto-scanning feature in Spring. The base-package is indicate where are your components stored, Spring will scan this folder and find out the bean (annotated with @Component) and register it in Spring container.
So I am wondering what is the use of @Configuration
then if @Controller
will register my beans without the need to declare them in the spring configuration XML file.
@Configuration 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. @Component Indicates that an annotated class is a "component".
Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. A @Configuration is also a @Component, but a @Component cannot act like a @Configuration.
AutoConfiguration classes are run last (meaning after all regular non-autoconfiguration classes) while the order in which Configuration classes are run is indeterminate (except if we use ordering annotations like @Ordered ) To declare a class as an AutoConfiguration they need to be specified as such in the spring.
@Component and @ComponentScan are for different purposes. @Component indicates that a class might be a candidate for creating a bean. It's like putting a hand up. @ComponentScan is searching packages for Components.
From Book Pro Spring Integration
@Configuration
classes are just like regular @Components
classes, except that methods annotated with @Bean
are used to factory beans. Note that a @Component
with @Bean
annotated methods works the same way, except that scopes are not respected and the @Bean
methods are re-invoked (no caching in play), so @Configuration
is preferred, even though it requires CGLIB
Here is difference with full example :-
//@Configuration or @Component
public static class Config {
@Bean
public A a() {
return new A();
}
//**please see a() method called inside b() method**
@Bean
public B b() {
return new B(a());
}
}
1) Here if Config class annotated with @configuration , than a() method and b() method , both will be called once .
2)Here if Config class annotated with @component , than b() method will be called once but a() method will be called twice .
Problem in (2) :- since we have noticed the problem with @component annotation . This second configuration (2) is totally incorrect because spring will create a singleton bean of A, but B will obtain another instance of A which is out of the spring context control.
Solution :- we can use @autowired annotation with @component annotation inside Config class .
@Component
public static class Config {
@Autowired
A a;
@Bean
public A a() {
return new A();
}
@Bean
public B b() {
return new B(a);
}
}
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