09 · Spring Framework and the IoC container / LESSON 42 OF 102
Bean lifecycle and Java configuration
A managed bean moves through creation, dependency population, initialization, use, and shutdown.
Before this lesson: Singleton, prototype, and request scopes
Understand the topic
@PostConstruct runs after dependency injection; @PreDestroy handles normal managed shutdown. Avoid doing slow remote calls in constructors or assuming shutdown hooks run after a forced process kill. Resource cleanup still needs external durability guarantees.
@Bean is useful for third-party classes you cannot annotate. With proxyBeanMethods=false, calls between configuration methods are ordinary Java calls; request dependencies as bean-method parameters to avoid accidental duplicate objects.
EXAMPLE · JAVA
LifecycleConfig.java
import java.time.Clock;
import org.springframework.context.annotation.*;
@Configuration(proxyBeanMethods = false)
public class LifecycleConfig {
@Bean Clock clock() { return Clock.systemUTC(); }
@Bean java.util.function.Supplier<java.time.Instant> now(Clock clock) {
return clock::instant;
}
}What to expect
The supplier receives the managed Clock as a method parameter without relying on intercepted calls between configuration methods.
YOUR TURN
Put it into practice
- 01Add initialization and destruction logging to a local resource bean.
- 02Close the context normally and compare with an abrupt process termination.
- 03Verify: Required data is durable before shutdown and cleanup does not assume every termination is graceful.
Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.
UNDER THE HOOD
Build the mental model
A managed bean moves through creation, dependency population, initialization, use, and shutdown.
- 01@PostConstruct runs after dependency injection; @PreDestroy handles normal managed shutdown. Avoid doing slow remote calls in constructors or assuming shutdown hooks run after a forced process kill. Resource cleanup still needs external durability guarantees.
- 02@Bean is useful for third-party classes you cannot annotate. With proxyBeanMethods=false, calls between configuration methods are ordinary Java calls; request dependencies as bean-method parameters to avoid accidental duplicate objects.
- 03Trace the example: The supplier receives the managed Clock as a method parameter without relying on intercepted calls between configuration methods.
A mistake worth avoiding
Calling a @Bean method creates a duplicate object with proxying disabled.
DEBUGGING ROOM
Something broke. Where do you look?
Calling a @Bean method creates a duplicate object with proxying disabled.
Before revealing the diagnosis, write down your first investigation step.
CHECK YOUR UNDERSTANDING
Is @PreDestroy guaranteed after a forced process kill?
BUILD IT YOURSELF
Add initialization and destruction logging to a local resource bean.
- 1.Add initialization and destruction logging to a local resource bean.
- 2.Close the context normally and compare with an abrupt process termination.
- 3.Reproduce this failure, then repair it: Calling a @Bean method creates a duplicate object with proxying disabled.
Acceptance checklist
Lab completion is your self-report. Knowledge checks do not certify project quality.
RETRIEVE IT FROM MEMORY
Is @PreDestroy guaranteed after a forced process kill?
Revisit this lesson in three days after a successful check or lab.
Ready for the next step?
Required data is durable before shutdown and cleanup does not assume every termination is graceful.
Reading is separate from passing a knowledge check or completing a lab.