Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between @Component and @Configuration in Spring 3

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.

like image 239
Anand Avatar asked Aug 10 '12 13:08

Anand


People also ask

What is difference between @configuration and component?

@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".

Can we use @configuration and @component together?

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.

What is the difference between @configuration and AutoConfiguration?

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.

What is difference between @component and @ComponentScan?

@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.


2 Answers

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

like image 57
Javaboy Avatar answered Nov 09 '22 03:11

Javaboy


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);
    }
}
like image 24
Vijay Avatar answered Nov 09 '22 04:11

Vijay