All modules

Module 2 · Understand

Spring from First Principles

Start with ordinary Java object construction, follow Spring configuration from XML to annotations, and inspect how the container turns bean definitions into managed objects.

Foundation14 chapters345 minutesPrerequisite: Spring Boot fundamentals
Start module

Chapter plan

What you will complete

01Why Spring Exists: From new to a ContainerBegin with a tightly coupled Java object graph and discover the exact construction, configuration, lifecycle, and testability problems the Spring container solves.Object constructionInversion of ControlDependency Injection 22 min02From XML Beans to Modern Spring ConfigurationDefine one object graph with XML, @Configuration and @Bean, and component scanning—and see how every style produces bean-definition metadata for the same container.XML bean metadataJava configurationComponent scanning 28 min03The IoC ContainerMeet the ApplicationContext: the registry that creates your objects, resolves their dependencies, and remembers every singleton it made.ApplicationContextBeanFactoryInversion of Control 15 min04Bean Definitions & MetadataComponent scanning produces BeanDefinition metadata first and instantiates objects later; the plan precedes the instances.BeanDefinitionMetadataClass Scanning 15 min05Stereotype Annotations@Component, @Service, @Repository, and @Controller all register beans; the differences are declared intent plus the machinery specific stereotypes switch on.@Component@Service@Repository 15 min06Java Configuration (@Bean)@Configuration and @Bean bring classes you do not own—SDK clients, clocks, mappers—under container lifecycle and injection control.@Configuration@BeanThird-party Beans 20 min07Dependency Injection FormsConstructor, setter, and field injection differ in what they make impossible; constructor injection makes incomplete objects impossible.Constructor InjectionField InjectionTestability 20 min08Resolving AmbiguityWhen several beans share one type, resolve the conflict deliberately with @Primary, @Qualifier, parameter-name matching, or collection injection.@Primary@QualifierList<T> Injection 20 min09Bean ScopesSingleton is the default and usually right; prototype and web scopes need a reason plus a strategy for crossing scope boundaries.SingletonPrototypeScoped Proxies 15 min10The Bean LifecycleA bean is instantiated, populated, post-processed, initialized, used, and destroyed—in that order, with a hook at each seam.@PostConstruct@PreDestroyInitializingBean 25 min11BeanPostProcessor & BeanFactoryPostProcessorUnderstand the extension points that inspect bean definitions, alter metadata, and wrap finished beans before application code receives them.Separate definition-time changes from instance-time wrappingRecognize proxy creation as post-processingAvoid business logic inside container extension points 35 min12Proxy Boundaries and Self-InvocationExplain why internal calls bypass transactional, async, security, and caching advice, then structure services so proxy boundaries are crossed intentionally.Know which annotations depend on Spring proxiesProve self-invocation bypasses adviceMove advised behavior behind another bean or explicit boundary 40 min13Diagnose Circular DependenciesFind constructor cycles, understand why early references are fragile, and redesign orchestration instead of hiding the dependency loop.Detect a real constructor dependency cycleIdentify the responsibility that should own orchestrationUse events, ports, or a coordinator instead of lazy self-references 30 min14Production Implementation: Inspectable Spring WiringAssemble a real Controller -> Service -> Repository slice with constructor injection, @Configuration beans, and lifecycle hooks you can observe at startup.Wire collaborators with constructors instead of field injectionRegister third-party types through @Configuration and @BeanObserve bean creation order, proxy wrapping, and shutdown callbacks 45 min

Module outcome

Explain why Spring exists, how configuration metadata becomes BeanDefinition objects, and how the container creates, wires, initializes, and sometimes proxies a bean.

Mental model

Configuration is a set of object recipes. The container reads those recipes, resolves their dependencies, creates the objects, applies lifecycle extensions, and exposes the resulting beans through an ApplicationContext.

Module checkpoint

Configurable Notifications

Assemble a notification use case with constructor injection, explicit adapter selection, lifecycle evidence, and no circular dependencies.

  • A plain unit test constructs the service without Spring
  • A qualifier or collection injection selects the intended adapter
  • Lifecycle logs prove construction, post-processing, initialization, and shutdown order

Final module assessment

Ready to prove mastery?

Complete all 50 questions after finishing the module lessons and checkpoint.

Take module quiz