Difference between @component & @bean annotations in Spring boot | Interview Question | Code Decode

preview_player
Показать описание
In this video of code decode we have explained @bean vs @component difference in spring boot

Aspect
@Component
@Bean
Purpose/Role
Class level Spring component annotation.
Method-level annotation for creating beans.
Typical Use Case
Represents a general-purpose bean.
Used to explicitly declare individual beans.
Creation of Instances
Automatically created and managed by Spring.
Explicitly defined in a configuration class.
Component Scanning
Detected and registered during component scanning.
Dependency Injection
Can be injected into other components through Spring's IoC container.
Can be injected into other components; the method returning the bean is invoked.

Lifecycle Management
Spring manages the complete lifecycle of components.
Spring manages the lifecycle of beans, but individual methods in a @Configuration class are not managed by Spring.
Dependencies
Simpler to use for straightforward scenarios without intricate configurations
Well-suited for handling complex dependencies and conditional bean creation(using @Conditional)
Customization
Provides less explicit control, suitable for default configurations
Offers fine-grained control over bean instantiation and configuration
Naming Conventions
Class name is used as the default bean name (can be customized with @Component("customName"))
Method name determines the bean name (unless specified explicitly)

Hibernate Interview Questions and Answers:

Spring Boot Interview Questions and Answers:

Subscriber and Follow Code Decode

#pathvariable #codedecode #springboot
Рекомендации по теме
Комментарии
Автор

The main difference is with @Bean you are able to instantiate beans for third party library objects where you do not have the access to the source code to add @Component annotation. for example PasswordEncoder.

@Bean annotation provides more control over how you want to create the bean.

akashshaw
Автор

Ma'am please continue doing such videos.... thanks for covering whole concept in depth

GreenAspirant
Автор

Your videos are really really very helpful 🎉

surajupadhyay
Автор

It was very clear and easy to remember also.Glad am the first person to comment 😊

bluesibilings
Автор

Its better to show writing full code flow to understand littlest bit clearly

maheshh
Автор

I think conditional can be used with component or service annotations.

killingmachinelp