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
Subscribe to:
Post Comments (Atom)
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...
-
The strictfp keyword is used to force the precision of floating point calculations (float or double) in Java conform to IEEE’s 754 standard...
-
In construction. Spring test annotations: @SpringBooTest @DataJpaTest @TestPropertySource @ActiveProfiles @Sql @SpringBootTest It is used f...
-
Spring Security framework has two key context objects which an application must interact with: SecurityContextHolder – SecurityContex...

No comments:
Post a Comment