gaurav sood
gaurav sood

Reputation: 1193

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

@ComponentScan creates beans using both @Configuration and @Component. Both these annotations work fine when swapped. What is the difference then?

Upvotes: 119

Views: 158326

Answers (8)

DevMateus
DevMateus

Reputation: 301

@Configuration and @Component are both annotations used in Spring Framework, but they serve different purposes.

@Configuration is a type-level annotation that 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. Classes annotated with @Configuration are usually used as sources of bean definitions.

@Component is a class-level annotation that 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. It is a generic stereotype for any Spring-managed component or bean.

In simple terms, @Configuration is used for defining beans and their dependencies, whereas @Component is used for general-purpose auto-detection of components.

In most cases, you would use @Configuration to define beans for complex configurations and @Component to define simple beans that are used throughout your application.

Here are some official references that you may find helpful:

Spring Framework documentation: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-java

@Configuration JavaDoc: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

@Component JavaDoc: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Component.html

Upvotes: 4

Deekshith Anand
Deekshith Anand

Reputation: 2662

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 invoke it multiple times, it returns the same bean that was created by Spring - sort of 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 to as bean light mode processing). So, in light 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!

Upvotes: 70

riyadhrazzaq
riyadhrazzaq

Reputation: 49

I am extending on @reus's answer.

  1. @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.
  2. If you look at the @Configuration class, you will see that it is meta-annotated with @Component.
@Target(value=TYPE)
 @Retention(value=RUNTIME)
 @Documented
 @Component
public @interface Configuration
  1. @Bean is enables us to define the dependency in any way we like, this is why the @Bean annotation goes above a methods and we manually create a bean object and return it from that method. @Component enables us to define a dependency quickly, that is why @Component goes above classes. We only inject it wherever we need.

Collectively these 3 points says that- to quickly define a bean, we can annotate the class with @Component. To define a bean as we like (support custom requirements), we can write the bean definition using @Bean inside a @Configuration annotated class.

Upvotes: 2

Sachin Ghumbre
Sachin Ghumbre

Reputation: 187

@Configuration - It is like beans.xml but Java-based bean configuration. It means class annotated with this annotation is the place where beans are configured and will be a candidate for auto-detection. In this class, methods are annotated with @Bean which return an object of the class.

Example:

@Configuration
public class ConfigClass {

    @Bean
    public UserClass getObject() {
        return new UserClass();
    }
}

@Component - You cannot autowire (@Autowired) any class if it is not marked with @Component. It means when you want to autowire any class using annotation that class should be annotated with @Component.

Example:

@Component
public class A { .... }

public class B { 
    @Autowired
    A a;
    .....
    .....
}

Spring Document for reference: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

Upvotes: 17

reos
reos

Reputation: 8324

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

Upvotes: 111

ha9u63a7
ha9u63a7

Reputation: 6814

@Component is imported by default with @Configuration. controllers, service, and repostory are children components (along with Configuration). They are also candidate for auto-detection.

Upvotes: 2

Juan Rada
Juan Rada

Reputation: 3766

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

Upvotes: 42

Nandish
Nandish

Reputation: 326

Apart from the differences highlighted by reos.

The reason why @Configuration cannot be replaced by @Component is as below:

The difference is in how the inter bean dependency is handled. Refer the link for a detailed explanation with example: Difference between Configuration and Component

Upvotes: -3

Related Questions