Challenge - 5 Problems
Spring Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
When is a method annotated with @PostConstruct called?
In a Spring Boot application, what is the correct time when a method annotated with
@PostConstruct is executed?Attempts:
2 left
💡 Hint
Think about when you want to run setup code after all dependencies are ready.
✗ Incorrect
The @PostConstruct method runs once the bean is fully created and all dependencies are injected. It is used to perform initialization tasks before the bean is used.
❓ component_behavior
intermediate2:00remaining
What happens when a method is annotated with @PreDestroy?
In a Spring Boot bean, when is a method annotated with
@PreDestroy executed?Attempts:
2 left
💡 Hint
Think about cleanup tasks before the bean disappears.
✗ Incorrect
The @PreDestroy method runs just before the bean is destroyed by the Spring container. It is used to release resources or perform cleanup.
📝 Syntax
advanced2:30remaining
Identify the correct usage of @PostConstruct in a Spring Boot bean
Which of the following code snippets correctly uses
@PostConstruct in a Spring Boot component?Attempts:
2 left
💡 Hint
Remember the method must be void, no parameters, and non-static.
✗ Incorrect
The @PostConstruct method must be void, have no parameters, and not be static. Option C meets all these requirements.
🔧 Debug
advanced2:30remaining
Why does the @PreDestroy method not run in this Spring Boot application?
Consider this Spring Boot bean:
public class MyService {
@PreDestroy
public void cleanup() {
System.out.println("Cleaning up resources");
}
}
Why might the
cleanup method never be called when the application stops?Attempts:
2 left
💡 Hint
Check if Spring knows about this bean to manage its lifecycle.
✗ Incorrect
If a class is not registered as a Spring bean (e.g., missing @Component), Spring does not manage its lifecycle, so @PreDestroy methods won't be called.
🧠 Conceptual
expert3:00remaining
What is the effect of multiple @PostConstruct methods in a single Spring bean?
If a Spring bean class has two methods both annotated with
@PostConstruct, what will happen when the bean is initialized?Attempts:
2 left
💡 Hint
Think about how Java handles multiple lifecycle callbacks in one class.
✗ Incorrect
Spring calls all methods annotated with @PostConstruct in a bean, but the order is not guaranteed. Both methods will run during initialization.