Lab 12: Spring-Style DI

Time: 60 minutes | Level: Architect | Docker: docker run -it --rm zchencow/innozverse-java:latest bash


Overview

Understand Spring Boot's magic by building it from scratch: a manual DI container using custom annotations and reflection, property binding, condition evaluation, and application context lifecycle — all without any Spring dependency.


Step 1: Spring Boot Internals Overview

Spring Boot startup sequence:
  1. SpringApplication.run()
     ├── Detect application type (Servlet/Reactive/None)
     ├── Load ApplicationContext initializers
     ├── Load ApplicationListeners
     └── Create ApplicationContext
  2. Context refresh:
     ├── BeanDefinition scanning (@Component, @Service, @Repository)
     ├── BeanFactory post-processing (@Configuration + @Bean)
     ├── Condition evaluation (@ConditionalOnProperty, @ConditionalOnClass)
     ├── Bean instantiation (constructor injection)
     ├── Dependency injection (@Autowired, @Value)
     └── Lifecycle callbacks (@PostConstruct, ApplicationRunner)
  3. Embed Tomcat/Jetty/Undertow

What we'll build:
  @Component → register bean
  @Inject    → field injection
  @Value     → property binding
  Condition  → conditional bean creation
  BeanFactory lifecycle

Step 2: Custom Annotations


Step 3: BeanDefinition and BeanFactory


Step 4: Bean Classes


Step 5: Property Binding


Step 6: Application Context Lifecycle


Step 7: Condition Evaluation


Step 8: Capstone — Manual DI Container

📸 Verified Output:


Summary

Spring Concept
Manual Equivalent
Mechanism

@Component

Custom @Component annotation

@Retention(RUNTIME)

@Autowired

Custom @Inject annotation

Field.set() via reflection

BeanFactory

SimpleBeanFactory

Map<String, Object>

ApplicationContext

ApplicationContext class

Lifecycle phases

@Value

Custom @Value + property resolver

Properties lookup

@ConditionalOnProperty

Condition interface

Properties.getProperty()

@PostConstruct

Custom annotation

Method.invoke()

Configuration binding

bind(props, prefix, class)

Reflection field assignment

Last updated