0
0
Spring Bootframework~10 mins

@PostConstruct and @PreDestroy in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A@Service
B@PreDestroy
C@Autowired
D@PostConstruct
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
2fill in blank
medium

Complete 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'
A@PreDestroy
B@PostConstruct
C@Bean
D@Component
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
3fill in blank
hard

Fix 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'
A@Autowired
B@PreDestroy
C@PostConstruct
D@Service
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PreDestroy which runs before bean destruction
Missing the annotation causing the method not to run automatically
4fill in blank
hard

Fill 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'
A@PostConstruct
B@Autowired
C@PreDestroy
D@Component
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the annotations between methods
Using @Autowired instead of lifecycle annotations
5fill in blank
hard

Fill 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'
APostConstruct
BPreDestroy
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong packages
Forgetting to annotate setup() with @PostConstruct
Using @Component as an annotation for methods