Friday, 16 March 2018

Difference between @Bean and @Component


@Component(Spring stereotype Annotation):

It modifies classes. All components can be auto-wired in Spring context as the Component scan is turned on.

@Bean: 

It modifies methods in a @Configuration class. The instance returned by this method will be registered in the Spring application context. By default, the bean has the same name as the method name. It is an explicit way to declare a bean in the IoC container. They are injected by the framework just like constructor dependencies are resolved

When source code is not available, it is not able to modify a class with a @Component.  So it cannot be auto-wired. In this case, using @Bean is the option.

@Bean methods and inter-dependencies



1. Injecting by type
The dependent type has only one instance. It can be injected by the type directly.
2. Injecting by name
If there are more that one instance of the same type for a target injection point. 
3.Injecting by name to matching qualifier
4. Injecting by qualifiers on both sides.




References:

SPRING STEREOTYPE ANNOTATIONS

Spring Method @Bean Dependency Injection

No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...