@ComponentScan
creates beans using both @Configuration
and @Component
. Both these annotations work fine when swapped. What is the diff
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