Skip to main content

Configuration, Profiles, Scopes, and Lifecycle

Cert Focus

You should know how beans are registered, configured, selected by environment, scoped, initialized, and destroyed.

Must Know

  • @Configuration classes declare bean definitions.
  • @Bean registers the return value of a method as a Spring bean.
  • @Value injects simple property values.
  • @ConfigurationProperties binds 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.

Exam Trap

Spring singleton is not the classic Java Singleton pattern. It means one bean instance per Spring container.

Self-Check

  • Can I explain @Component vs @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.