What is the purpose of the @PostConstruct annotation in Spring Boot?
It marks a method to be executed once immediately after the bean's initialization and dependency injection are complete.
Click to reveal answer
beginner
When is a method annotated with @PreDestroy called in a Spring Boot application?
It is called just before the bean is removed from the container, typically during application shutdown, to allow cleanup.
Click to reveal answer
intermediate
Can @PostConstruct and @PreDestroy methods have parameters?
No, methods annotated with @PostConstruct and @PreDestroy must have no parameters and return void.
Click to reveal answer
intermediate
How do @PostConstruct and @PreDestroy relate to the bean lifecycle in Spring Boot?
They provide hooks to run custom code right after bean creation (@PostConstruct) and right before bean destruction (@PreDestroy), managing setup and cleanup.
Click to reveal answer
intermediate
What happens if you forget to add @PreDestroy on a cleanup method in Spring Boot?
The cleanup code won't run automatically on shutdown, which may cause resource leaks like open connections or files.
Click to reveal answer
Which annotation runs a method after Spring Boot bean initialization?
A@Component
B@PreDestroy
C@Autowired
D@PostConstruct
✗ Incorrect
@PostConstruct runs after the bean is fully initialized.
What is the correct signature for a method annotated with @PreDestroy?
Apublic void cleanup()
Bpublic int cleanup(int code)
Cpublic String cleanup(String msg)
Dpublic void cleanup(String param)
✗ Incorrect
@PreDestroy methods must have no parameters and return void.
When is a @PreDestroy method executed in Spring Boot?
ABefore bean destruction
BAfter bean initialization
CDuring bean creation
DWhen a bean is injected
✗ Incorrect
@PreDestroy runs just before the bean is destroyed.
What happens if you don't use @PostConstruct for initialization code?
AThe code runs before bean creation
BThe code runs after bean destruction
CThe code might not run automatically after bean setup
DThe code runs multiple times
✗ Incorrect
Without @PostConstruct, initialization code won't run automatically after dependencies are injected.
Which of these is NOT true about @PostConstruct and @PreDestroy?
AThey help manage bean lifecycle events
BThey can have parameters in their methods
C@PostConstruct runs after dependencies are injected
D@PreDestroy runs before bean removal
✗ Incorrect
Methods annotated with these must have no parameters.
Explain how @PostConstruct and @PreDestroy annotations help manage a Spring Boot bean's lifecycle.
Think about when you want to prepare and clean up resources in your app.
You got /4 concepts.
Describe what could happen if you omit @PreDestroy on a method that closes resources in a Spring Boot app.
Consider what happens if you never close a door after entering a room.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the @PostConstruct annotation in a Spring Boot application?
easy
A. To run a method immediately after the bean is created and dependencies are injected
B. To mark a method that should run before the bean is destroyed
C. To define a method that runs only when the application starts
D. To schedule a method to run repeatedly at fixed intervals
Solution
Step 1: Understand the role of @PostConstruct
@PostConstruct is used to run initialization code after the Spring bean is fully created and dependencies are set.
Step 2: Compare with other lifecycle annotations
@PreDestroy runs before bean removal, and scheduling annotations run repeatedly, so they don't match @PostConstruct's purpose.
Final Answer:
To run a method immediately after the bean is created and dependencies are injected -> Option A
Quick Check:
@PostConstruct = run after bean creation [OK]
Hint: Remember: PostConstruct runs right after bean setup [OK]
Common Mistakes:
Confusing @PostConstruct with @PreDestroy
Thinking @PostConstruct runs before bean creation
Assuming it schedules repeated tasks
2. Which of the following is the correct way to use @PreDestroy in a Spring Boot bean?
easy
A. Place @PreDestroy on a static method to run after application shutdown
B. Annotate a method with @PreDestroy to run cleanup code before bean destruction
C. Use @PreDestroy on a constructor to initialize resources
D. Annotate the class with @PreDestroy to mark it for destruction
Solution
Step 1: Identify correct usage of @PreDestroy
@PreDestroy is placed on a method to run cleanup code before the bean is destroyed.
Step 2: Eliminate incorrect options
@PreDestroy cannot annotate a class or constructor, and static methods are not lifecycle callbacks.
Final Answer:
Annotate a method with @PreDestroy to run cleanup code before bean destruction -> Option B
Quick Check:
@PreDestroy = method cleanup before bean removal [OK]
Hint: Use @PreDestroy only on instance methods for cleanup [OK]
Common Mistakes:
Annotating the class instead of a method
Using @PreDestroy on constructors
Marking static methods with @PreDestroy
3. Consider the following Spring Boot bean code snippet:
public class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean is initialized");
}
@PreDestroy
public void cleanup() {
System.out.println("Bean is about to be destroyed");
}
}
What will be printed when the application context starts and then shuts down?
medium
A. Bean is about to be destroyed Bean is initialized
B. Only Bean is initialized
C. Bean is initialized Bean is about to be destroyed
D. Only Bean is about to be destroyed
Solution
Step 1: Understand @PostConstruct behavior on startup
The init() method runs after bean creation, printing "Bean is initialized" when the app context starts.
Step 2: Understand @PreDestroy behavior on shutdown
The cleanup() method runs before bean destruction, printing "Bean is about to be destroyed" when the app context shuts down.
Final Answer:
Bean is initialized Bean is about to be destroyed -> Option C
Quick Check:
Startup prints init, shutdown prints cleanup [OK]
Hint: PostConstruct runs on start, PreDestroy runs on shutdown [OK]
Common Mistakes:
Mixing order of printed messages
Expecting only one message to print
Thinking @PreDestroy runs on startup
4. Given this Spring Boot bean code:
public class ResourceBean {
@PostConstruct
public void setup() {
System.out.println("Setup resources");
}
@PreDestroy
public void cleanup() {
System.out.println("Cleanup resources");
}
public void start() {
System.out.println("Start method called");
}
}
When running the application, the developer notices that "Cleanup resources" is never printed on shutdown. What is the most likely cause?
medium
A. The @PostConstruct method prevents @PreDestroy from running
B. The @PreDestroy method must be static to run correctly
C. The cleanup() method must be called manually to run
D. The bean is not managed by Spring, so lifecycle annotations are ignored
Solution
Step 1: Check if bean is managed by Spring
@PostConstruct and @PreDestroy only work if Spring manages the bean lifecycle (e.g., via @Component or @Bean).
Step 2: Understand why cleanup isn't called
If the bean is created manually (new keyword), Spring won't call lifecycle methods, so cleanup() won't run automatically.
Final Answer:
The bean is not managed by Spring, so lifecycle annotations are ignored -> Option D
Quick Check:
Lifecycle annotations need Spring-managed beans [OK]
Hint: Lifecycle methods run only on Spring-managed beans [OK]
Common Mistakes:
Thinking @PreDestroy must be static
Assuming @PostConstruct blocks @PreDestroy
Believing cleanup() runs without Spring management
5. You want to ensure a Spring Boot bean opens a database connection after creation and closes it before destruction. Which code snippet correctly uses @PostConstruct and @PreDestroy to achieve this?
hard
A. public class DbBean {
private Connection conn;
@PostConstruct
public void open() {
conn = DriverManager.getConnection("url");
System.out.println("Connection opened");
}
@PreDestroy
public void close() {
conn.close();
System.out.println("Connection closed");
}
}
B. public class DbBean {
private Connection conn;
public void open() {
conn = DriverManager.getConnection("url");
System.out.println("Connection opened");
}
public void close() {
conn.close();
System.out.println("Connection closed");
}
}
C. public class DbBean {
private Connection conn;
@PreDestroy
public void open() {
conn = DriverManager.getConnection("url");
System.out.println("Connection opened");
}
@PostConstruct
public void close() {
conn.close();
System.out.println("Connection closed");
}
}
D. public class DbBean {
private Connection conn;
@PostConstruct
public void open() {
System.out.println("Connection opened");
}
@PreDestroy
public void close() {
System.out.println("Connection closed");
}
}
Solution
Step 1: Identify correct lifecycle method usage
@PostConstruct should open the connection after bean creation; @PreDestroy should close it before destruction.
Step 2: Check code correctness
public class DbBean {
private Connection conn;
@PostConstruct
public void open() {
conn = DriverManager.getConnection("url");
System.out.println("Connection opened");
}
@PreDestroy
public void close() {
conn.close();
System.out.println("Connection closed");
}
} correctly uses @PostConstruct to open and @PreDestroy to close the connection with proper method calls.
Final Answer:
public class DbBean {
private Connection conn;
@PostConstruct
public void open() {
conn = DriverManager.getConnection("url");
System.out.println("Connection opened");
}
@PreDestroy
public void close() {
conn.close();
System.out.println("Connection closed");
}
} -> Option A
Quick Check:
Open after creation, close before destruction [OK]
Hint: Open in @PostConstruct, close in @PreDestroy for resource management [OK]
Common Mistakes:
Swapping @PostConstruct and @PreDestroy methods
Not calling connection methods inside lifecycle methods
Missing lifecycle annotations on resource methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.