Bird
Raised Fist0
Spring Bootframework~20 mins

@PostConstruct and @PreDestroy in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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?
ARight after the bean is fully initialized and dependencies are injected, before the bean is ready to use.
BOnly when the application context is closed.
CAfter the bean is destroyed and removed from the context.
DBefore the bean is created and dependencies are injected.
Attempts:
2 left
💡 Hint
Think about when you want to run setup code after all dependencies are ready.
❓ component_behavior
intermediate
2:00remaining
What happens when a method is annotated with @PreDestroy?
In a Spring Boot bean, when is a method annotated with @PreDestroy executed?
AAfter the bean is fully initialized and ready to use.
BWhen the bean is created and dependencies are injected.
CWhen the application starts.
DRight before the bean is destroyed and removed from the Spring context.
Attempts:
2 left
💡 Hint
Think about cleanup tasks before the bean disappears.
📝 Syntax
advanced
2: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?
A
public class MyBean {
  @PostConstruct
  public void init(String param) {
    System.out.println(param);
  }
}
B
public class MyBean {
  @PostConstruct
  private int init() {
    return 1;
  }
}
C
public class MyBean {
  @PostConstruct
  public void init() {
    System.out.println("Bean initialized");
  }
}
D
public class MyBean {
  @PostConstruct
  public static void init() {
    System.out.println("Static init");
  }
}
Attempts:
2 left
💡 Hint
Remember the method must be void, no parameters, and non-static.
🔧 Debug
advanced
2: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?
AThe bean is not managed by Spring because it lacks a stereotype annotation like @Component.
B@PreDestroy methods only run on singleton beans, and this bean is prototype scoped.
CThe application context is not closed properly, so @PreDestroy is skipped.
DThe method is public and void, so it should run without issues.
Attempts:
2 left
💡 Hint
Check if Spring knows about this bean to manage its lifecycle.
🧠 Conceptual
expert
3: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?
AOnly the first declared @PostConstruct method will be called; the second is ignored.
BBoth methods will be called in an undefined order during bean initialization.
CSpring will throw a runtime exception due to multiple @PostConstruct methods.
DNeither method will be called because of the conflict.
Attempts:
2 left
💡 Hint
Think about how Java handles multiple lifecycle callbacks in one class.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To run a method immediately after the bean is created and dependencies are injected -> Option A
  4. 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

  1. Step 1: Identify correct usage of @PreDestroy

    @PreDestroy is placed on a method to run cleanup code before the bean is destroyed.
  2. Step 2: Eliminate incorrect options

    @PreDestroy cannot annotate a class or constructor, and static methods are not lifecycle callbacks.
  3. Final Answer:

    Annotate a method with @PreDestroy to run cleanup code before bean destruction -> Option B
  4. 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

  1. Step 1: Understand @PostConstruct behavior on startup

    The init() method runs after bean creation, printing "Bean is initialized" when the app context starts.
  2. 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.
  3. Final Answer:

    Bean is initialized
    Bean is about to be destroyed
    -> Option C
  4. 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

  1. 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).
  2. 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.
  3. Final Answer:

    The bean is not managed by Spring, so lifecycle annotations are ignored -> Option D
  4. 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

  1. Step 1: Identify correct lifecycle method usage

    @PostConstruct should open the connection after bean creation; @PreDestroy should close it before destruction.
  2. 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.
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard