In this video, we are going to discuss about the @Bean.
Spring Annotation
Spring Boot Annotation
learn to use @Bean annotation in spring boot.
- @Bean annotation is used to define a method that produces a bean to be managed by the Spring container.
- A bean is an object that is instantiated and managed by the Spring IoC (Inversion of Control) container.
- When you annotate a method with @Bean, Spring Boot will automatically detect and register that method as a bean during the component scanning process. This allows you to create and configure beans in a Java-based configuration class.
- @Bean annotation provides init Method and destroy Method attribute to perform certain actions after bean initialization or before bean destruction by a container.
1. Bean Definition: When you annotate a method with @Bean, it signals to the Spring container that the method will create and return an instance of a bean. The container will then manage the lifecycle of this bean, handling its instantiation, configuration, and destruction.
2. Customization of Bean Configuration: @Bean to customize the configuration of a bean, such as setting properties, injecting dependencies, or applying specific initialization logic.
3. Dependency Injection: Beans defined with @Bean can be injected into other beans or components using dependency injection. This promotes a loosely coupled design, where components rely on abstractions (interfaces or abstract classes) rather than concrete implementations.
4. Programmatic Configuration: @Bean is commonly used in Java-based configuration classes (annotated with @Configuration). This provides a programmatic and type-safe way to configure beans compared to XML-based configurations.
5. Customizing Bean Names: @Bean annotation allows you to specify a custom name for the bean, which can be useful in cases where you need to refer to the bean by a specific name.
@Bean annotation provides a way to declare and customize beans in a Spring application, enabling the Spring IoC container to manage their lifecycle and facilitate dependency injection.
#springboot #spring #Springannotation
Негізгі бет What is @Bean Annotation? Purpose of @Bean with example - Spring & Spring Boot Annotations Series #8
Пікірлер: 2