What is the difference between @Configuration and @Component in Spring?
SpringSpring Problem Overview
@ComponentScan
creates beans using both @Configuration
and @Component
. Both these annotations work fine when swapped. What is the difference then?
Spring Solutions
Solution 1 - Spring
> @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:
A @Configuration is also a @Component, but a @Component cannot act like a @Configuration.
Solution 2 - Spring
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
Solution 3 - Spring
@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
Solution 4 - 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!
Solution 5 - Spring
@Component
is imported by default with @Configuration
. controllers, service, and repostory are children components (along with Configuration). They are also candidate for auto-detection.
Solution 6 - Spring
I am extending on @reus's answer.
- @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. - 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
@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.
Solution 7 - Spring
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