Configuration, Profiles, Scopes, and Lifecycle
Cert Focus
You should know how beans are registered, configured, selected by environment, scoped, initialized, and destroyed.
Must Know
@Configurationclasses declare bean definitions.@Beanregisters the return value of a method as a Spring bean.@Valueinjects simple property values.@ConfigurationPropertiesbinds structured configuration.- Profiles activate environment-specific beans or config.
- Singleton scope means one bean instance per Spring container.
- Prototype scope creates a new bean instance when requested.
- Web scopes include request and session.
- Bean lifecycle includes creation, dependency injection, initialization callbacks, and destruction callbacks.
Direct Book Links
- Java Configuration,
@Configuration, and@Bean - External Configuration,
@Value, and@ConfigurationProperties - Spring Profiles
- Bean Scopes
- Bean Lifecycle
- Week 2 Review
Exam Trap
Spring singleton is not the classic Java Singleton pattern. It means one bean instance per Spring container.
Self-Check
- Can I explain
@Componentvs@Bean? - Can I explain how profiles affect bean registration?
- Can I explain singleton vs prototype?
- Can I describe the bean lifecycle in order?
Model Answers
@Component is placed on a class and discovered by component scanning. @Bean is placed on a method, usually inside a configuration class, and the method return value becomes the bean.
Profiles control whether certain beans or configuration values are active for an environment such as dev, test, or prod.
Singleton means one bean instance per Spring container. Prototype means Spring creates a new instance each time the bean is requested from the container.
The common lifecycle is: instantiate bean, inject dependencies, run post-processors and aware callbacks, initialize bean, use bean, destroy singleton bean when the context shuts down.
Memory Sentence
Configuration tells Spring what to create; profiles and scopes tell Spring when and how to use it.