Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring

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

like image 599
gaurav sood Avatar asked Aug 26 '16 21:08

gaurav sood


People also ask

What is @configuration used for in Spring?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.

What is the difference between @configuration and autoconfiguration?

You can use @EnableAutoConfiguration annotation along with @Configuration annotation. It has two optional elements, exclude : if you want to exclude the auto-configuration of a class. excludeName : if you want to exclude the auto-configuration of a class using fully qualified name of class.

What is the difference between @component and @bean in Spring?

@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.

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

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

like image 60
reos Avatar answered Oct 13 '22 07:10

reos


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

like image 45
Juan Rada Avatar answered Oct 13 '22 09:10

Juan Rada