1. IoC core concepts
1. Inversion of Control
In traditional programming, objects are managed by themselves (actively created), while IoC transfers control to containers, and the container is responsible for the creation, assembly and management of objects to implement them.Reverse control of dependencies。
2. Dependency Injection
The core implementation of IoC is to inject dependencies through constructors, Setter methods or interfaces, rather than actively looking up or creating dependencies by objects.
2. Core Principle
1. Container architecture
graph TD A[Client] --> B[IoCcontainer] B --> C[BeanDefine registration] B --> D[Dependency analysis] B --> E[Lifecycle Management] C --> F[XML/annotation/JavaConfig] D --> G[Type Match/Automatic assembly] E --> H[initialization/Destroy callback]
2. Core Components
- BeanFactory: Basic container, providing DI support
- ApplicationContext: Extend containers, integrate AOPs, events, etc.
- BeanDefinition: Store the metadata of the bean (class name, scope, etc.)
3. Workflow
- Resource Positioning: Scan XML/annotation configuration
- Loading parsing: Generate BeanDefinition
- Register storage: Deposit into BeanDefinitionRegistry
- Dependency injection: Process @Autowired and other annotations
- initialization: Call @PostConstruct method
- Provide services: Get the instance through getBean()
3. Key implementation mechanism
1. Bean life cycle
Instantiation → Properties fill → BeanNameAware → BeanFactoryAware → ApplicationContextAware → PreInitialization → @PostConstruct → InitializingBean → init-method → Use period → @PreDestroy → DisposableBean → destroy-method
2. Dependency injection method
Injection method | Implementation Class | Features |
---|---|---|
Constructor injection | ConstructorResolver | Strong dependence, immutable |
Setter Injection | BeanWrapperImpl | Optional dependency, high flexibility |
Field Injection | AutowiredAnnotationBeanPostProcessor | The code is concise, but it destroys the encapsulation |
3. Circular dependency solution
Level 3 cache:
-
singletonFactories
(Beans that are not initialized) -
earlySingletonObjects
(Early quoted) -
singletonObjects
(Full Bean)
IV. Design Value
- Decoupling: Interaction between objects through interfaces, without paying attention to specific implementations
- Testability: Relying on Mockable, easy to unit testing
- Scalability: Modify the implementation class through configuration without changing the code
- Unified management: Centralized control of object life cycle and configuration
V. Typical application scenarios
// Traditional way (tight coupling)public class OrderService { private UserService userService = new UserServiceImpl(); } // IoC mode (loose coupling)public class OrderService { @Autowired private UserService userService; }
6. Key points of source code implementation
-
DefaultListableBeanFactory
: Core registration and acquisition implementation -
AbstractAutowireCapableBeanFactory
: Bean creation and injection -
AnnotationConfigApplicationContext
: Annotation drive container -
BeanPostProcessor
: Extension points (such as AOP proxy generation)
Summarize
Spring IoC byContainer managed object life cycleandDependency automatic injection, realizes loose coupling between components. Its core value lies in stripping the maintenance of object relationships from the code, and by configuring and declaring dependencies, the system is easier to maintain and expand. Understand itReflection mechanism、Cache PolicyandExtension point designIt is the key to mastering IoC implementation.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.