Beans are objects managed by Spring. Understanding their lifecycle helps you control how and when they are created, used, and destroyed.
Bean lifecycle overview in Spring Boot
public class MyBean {
// Initialization method
public void init() {
// code to run after bean creation
}
// Destruction method
public void destroy() {
// code to run before bean destruction
}
}The init method runs after the bean is created and dependencies are set.
The destroy method runs before the bean is removed from the container.
@PostConstruct and @PreDestroy annotations to mark init and destroy methods.@Component public class MyBean { @PostConstruct public void init() { System.out.println("Bean is initialized"); } @PreDestroy public void destroy() { System.out.println("Bean is being destroyed"); } }
@Component public class MyBean implements InitializingBean, DisposableBean { @Override public void afterPropertiesSet() { System.out.println("Bean is initialized"); } @Override public void destroy() { System.out.println("Bean is being destroyed"); } }
@Bean annotation.@Bean(initMethod = "init", destroyMethod = "cleanup") public MyBean myBean() { return new MyBean(); } public class MyBean { public void init() { System.out.println("Bean initialized"); } public void cleanup() { System.out.println("Bean destroyed"); } }
This Spring component prints messages when it is created and before it is destroyed, showing the bean lifecycle in action.
import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import org.springframework.stereotype.Component; @Component public class SimpleBean { @PostConstruct public void init() { System.out.println("SimpleBean initialized"); } @PreDestroy public void cleanup() { System.out.println("SimpleBean destroyed"); } }
Beans are created when the Spring container starts and destroyed when it shuts down.
Use @PostConstruct and @PreDestroy for simple lifecycle methods.
For prototype-scoped beans, Spring does not manage destruction automatically.
Spring beans have a lifecycle: creation, initialization, use, and destruction.
You can add code to run at init and destroy phases using annotations or interfaces.
Understanding this helps manage resources and app behavior cleanly.
Practice
Solution
Step 1: Understand Spring bean lifecycle phases
Spring beans go through creation, initialization, usage, and destruction phases during their lifecycle.Step 2: Compare options with lifecycle phases
Only Creation, initialization, use, and destruction correctly lists these phases. Other options describe unrelated programming concepts.Final Answer:
Creation, initialization, use, and destruction -> Option AQuick Check:
Bean lifecycle = Creation, init, use, destroy [OK]
- Confusing bean lifecycle with Java program lifecycle
- Mixing bean lifecycle with object-oriented concepts
- Thinking lifecycle only involves creation and destruction
Solution
Step 1: Identify annotation for post-initialization
@PostConstruct is the annotation used to mark a method to run after bean properties are set.Step 2: Eliminate other annotations
@PreDestroy runs before destruction, @Autowired injects dependencies, and @Bean defines a bean method.Final Answer:
@PostConstruct -> Option AQuick Check:
@PostConstruct runs after properties set [OK]
- Confusing @PreDestroy with @PostConstruct
- Using @Autowired for lifecycle methods
- Thinking @Bean runs after initialization
@Component
public class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean initialized");
}
}Solution
Step 1: Understand @PostConstruct behavior
The method annotated with @PostConstruct runs after the bean is created and properties set, printing "Bean initialized".Step 2: Check output possibilities
Since the method prints a message on init, output will be "Bean initialized". No errors or destruction messages occur here.Final Answer:
Bean initialized -> Option CQuick Check:
@PostConstruct prints "Bean initialized" [OK]
- Expecting output on bean destruction instead
- Thinking no output occurs without explicit call
- Confusing syntax causing compilation errors
cleanup() method is never called?
@Component
public class MyBean {
public void cleanup() {
System.out.println("Cleaning up");
}
}Solution
Step 1: Identify lifecycle annotation for destruction
The cleanup method should be annotated with @PreDestroy to run before bean destruction.Step 2: Check other options for correctness
@PostConstruct is for initialization, static or return value are not required for lifecycle methods.Final Answer:
Missing @PreDestroy annotation on cleanup method -> Option BQuick Check:
@PreDestroy needed to run cleanup before destroy [OK]
- Using @PostConstruct instead of @PreDestroy for cleanup
- Making cleanup static unnecessarily
- Expecting cleanup to run without annotation
Solution
Step 1: Understand lifecycle annotations usage
@PostConstruct runs after initialization, @PreDestroy runs before destruction; they must be on separate methods.Step 2: Evaluate other options
Annotating the same method with both annotations is invalid. @Autowired is for injection, not lifecycle. Implementing InitializingBean interface only handles post-initialization but not destruction.Final Answer:
Annotate one method with @PostConstruct and another with @PreDestroy -> Option DQuick Check:
Separate methods with @PostConstruct and @PreDestroy [OK]
- Trying to use one method for both init and destroy
- Confusing @Autowired with lifecycle annotations
- Using InitializingBean interface only
Ready to prove it?
Pass the test and this pattern moves unknown
