Junior — Middle
Kui sageli kutsutakse välja meetod, millele on märgitud @PreDestroy, Springis bini elutsükli lõppedes?
sobes.tech AI
Vastus AI-lt
The method annotated with @PreDestroy in Spring is called once at the end of the bean's lifecycle, i.e., before it is destroyed by the Spring container.
This occurs in the following cases:
- When the application or Spring context is stopped.
- When a bean scope is removed (e.g., for session or request scope).
@PreDestroy is used for releasing resources, closing connections, saving state, and other cleanup operations.
Example:
@Component
public class MyBean {
@PreDestroy
public void cleanup() {
System.out.println("Bean is being destroyed");
// release resources
}
}
Thus, the method with @PreDestroy is invoked exactly once during the bean's lifecycle, immediately before its destruction.