You are developing a Spring Boot application where a bean is required to perform a task immediately after the ApplicationContext has been started. How would you implement this?

  • Implement a custom event listener that listens for the ContextRefreshedEvent and executes the task.
  • Use the @EventListener annotation on a method and specify the event type as ApplicationStartedEvent.
  • Use the @OnStartup annotation on the bean's method.
  • Use the @PostConstruct annotation on the bean's method that needs to run after startup.
To execute a task immediately after the ApplicationContext has been started in a Spring Boot application, you can implement a custom event listener that listens for the ContextRefreshedEvent. This event is raised when the ApplicationContext is fully initialized and ready to use. You can then execute your task in response to this event.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *