Spring / Spring Interview questions II
Difference between @Bean and @Component annotation in spring.
Both aim to register target type in Spring container.
@Component auto scan and detects which configures the beans using classpath scanning while @Bean explicitly declares a single bean, rather than letting Spring do it automatically.
@Component does not decouple the declaration of the bean from the class definition where as @Bean decouples the declaration of the bean from the class definition.
@Bean is applicable to methods, whereas @Component is applicable to types.
@Component is a class level annotation where as @Bean is a method level annotation and name of the method serves as the bean name.
@Component need NOT use @Configuration annotation while @Bean annotation has to be used within the class annotated with @Configuration.
@Component has its specializations such as @Controller, @Repository and @Service while @Bean has no such specializations.
More Related questions...