Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mark a method that runs after the bean is created.
Spring Boot
public class MyService { [1] public void init() { System.out.println("Bean initialized"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PreDestroy instead of @PostConstruct
Forgetting to import the annotation
Placing the annotation on the class instead of the method
✗ Incorrect
The @PostConstruct annotation marks a method to be run after the bean is created and dependencies are injected.
2fill in blank
mediumComplete the code to mark a method that runs before the bean is destroyed.
Spring Boot
public class MyService { [1] public void cleanup() { System.out.println("Bean is being destroyed"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PostConstruct instead of @PreDestroy
Not having a method with void return type
Placing the annotation on a static method
✗ Incorrect
The @PreDestroy annotation marks a method to be run just before the bean is destroyed, useful for cleanup.
3fill in blank
hardFix the error in the code by choosing the correct annotation for the method that runs after bean creation.
Spring Boot
public class ResourceLoader { public void load() { System.out.println("Loading resources"); } [1] public void init() { load(); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PreDestroy which runs before bean destruction
Missing the annotation causing the method not to run automatically
✗ Incorrect
The init() method should be annotated with @PostConstruct to run after the bean is created.
4fill in blank
hardFill both blanks to correctly annotate methods for initialization and cleanup in a Spring bean.
Spring Boot
public class ConnectionManager { [1] public void start() { System.out.println("Connection started"); } [2] public void stop() { System.out.println("Connection stopped"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the annotations between methods
Using @Autowired instead of lifecycle annotations
✗ Incorrect
The start() method uses @PostConstruct to run after bean creation, and stop() uses @PreDestroy to run before destruction.
5fill in blank
hardFill all three blanks to create a Spring bean with proper lifecycle methods for initialization and destruction.
Spring Boot
import jakarta.annotation.[1]; import jakarta.annotation.[2]; public class CacheService { [3] public void setup() { System.out.println("Cache setup complete"); } @PreDestroy public void clear() { System.out.println("Cache cleared"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong packages
Forgetting to annotate setup() with @PostConstruct
Using @Component as an annotation for methods
✗ Incorrect
The imports must include PostConstruct and PreDestroy. The setup() method is annotated with @PostConstruct to run after bean creation.