How can you manage bean lifecycle events, such as initialization and destruction, in Spring Boot?
- By using the @Bean annotation with @PostConstruct and @PreDestroy methods.
- By declaring beans in an XML configuration file.
- By using the @Service annotation with initMethod and destroyMethod attributes.
- By configuring bean lifecycles in the main application class constructor.
You can manage bean lifecycle events, such as initialization and destruction, in Spring Boot by using the @Bean annotation along with @PostConstruct and @PreDestroy methods. These methods allow you to specify custom initialization and destruction logic for your beans. The other options mentioned (XML configuration, @Service with initMethod and destroyMethod, and configuring lifecycles in the main application class constructor) are not the recommended or common approaches for managing bean lifecycles in Spring Boot.
Loading...
Related Quiz
- What components are typically scanned and loaded when a test is annotated with @DataJpaTest in Spring Boot?
- What is JWT, and how is it used in conjunction with OAuth2 in Spring Boot?
- When using @Secured annotation, what is the format to specify the required authority?
- Suppose you are working on a Spring Boot project and need to ensure that certain fields in the incoming request payload are consistent with each other (e.g., startDate should be before endDate). How would you implement this validation?
- In Spring Boot, _____ annotation is used to map HTTP POST requests onto specific handler methods.