What is the difference between @Configuration and @Component in Spring?

There is a very subtle difference between them. Let me provide a very quick outlook to this.

Consider the below scenario:

@Configuration
public class MyConfig {

    @Bean
    public ServiceA aService(){
        return new ServiceA();
    }

    @Bean
    public ServiceB bService(){
        return new ServiceB(aService());
    }

}

Note that ServiceB bean has a dependecy on ServiceA and this is not autowired. Instead, the way it's written implies that a new instance is created which is not actually created by Spring. You, the programmer, did it with the new keyword instead.

So, if we do use @Configuration, then it uses CGLIB proxying, and in this situation it creates a singleton bean managed by the Spring context. If you multiple times, it returns the same bean that was created by Spring - sort of an autowiring effect.

Whereas if you use @Component, it won't do this proxying and will simply return a new instance every time the method is invoked, instead of providing the Spring managed instance. (Remember that a Spring bean is something that is managed by the Spring container, and, as a developer, it's your job is to pull them in, e.g. with @Autowired.

The same @Component effect can be achieved with @Configuration(proxyEnabled= false) (This is also referred as bean lite mode processing). So, in lite mode, you would end up doing something like this:

@Configuration(proxyEnabled = false) // Lite mode, same effect as @Component
public class MyConfig {

    @Bean
    public ServiceA aService() {
        return new ServiceA();
    }
    
    @Autowired
    @Bean
    public ServiceB bService(ServiceA aServiceBean){
        return new ServiceB(aServiceBean);
    }

}

Refer here for a more elaborate explanation

Hope that helps! Happy Coding!


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

@Configuration is meta-annotated with @Component, therefore @Configuration classes are candidates for component scanning

You can see more here:

http://docs.spring.io/spring-framework/docs/4.0.4.RELEASE/javadoc-api/org/springframework/context/annotation/Configuration.html

A @Configuration is also a @Component, but a @Component cannot act like a @Configuration.


Actually answer is not complete, is it true that:

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

But you do can create i.e MyConfiguration.java class then stereotype with @Component and add @Beans declaration to it. In this way it will looks as a configuration, main difference is that when annotated class with @Configuration @Bean annotated methods are proxy using CGLIB which made in code calls after the first one to return bean from context instead of execute method again and create another instance as happens when using @Component with @Bean

Tags:

Spring