关于IOC容器源码分析

master
xuchengsheng 2023-09-26 15:03:44 +08:00
parent a77cbbdf76
commit e853162690
9 changed files with 1268 additions and 7 deletions

View File

@ -33,8 +33,7 @@
## 🌱Spring 源码阅读系列
+ IOC容器
+ 关于IOC容器中获取Bean的过程源码分析
+ [关于IOC容器源码分析](spring-core-ioc/README.md)
+ 后置处理器与初始化
+ [关于BeanFactoryPostProcessor源码分析](spring-interface-beanFactoryPostProcessor/README.md)
@ -54,7 +53,6 @@
+ [关于InitializingBean源码分析](spring-interface-initializingBean/README.md)
+ [关于DisposableBean源码分析](spring-interface-disposableBean/README.md)
+ 核心注解
+ 关于@Bean源码分析
@ -62,7 +60,6 @@
+ 关于@Configuration源码分析
+ 关于@Import源码分析
+ 关于@PropertySource源码分析
+ Bean生命周期和工厂
+ 关于BeanFactory源码分析
@ -74,7 +71,6 @@
+ 关于BeanDefinition源码分析
+ 关于BeanDefinitionRegistry源码分析
+ 关于FactoryBean源码分析
+ 应用上下文相
- 关于ApplicationContext源码分析
@ -82,7 +78,6 @@
- 关于WebApplicationContext源码分析
- 关于ApplicationEventPublisher源码分析
- 关于ApplicationListener源码分析
+ 环境变量
- 关于Environment源码分析

View File

@ -39,6 +39,7 @@
<module>spring-interface-smartInstantiationAwareBeanPostProcessor</module>
<module>spring-interface-initializingBean</module>
<module>spring-interface-disposableBean</module>
<module>spring-core-ioc</module>
</modules>
<dependencies>

1192
spring-core-ioc/README.md Normal file

File diff suppressed because it is too large Load Diff

14
spring-core-ioc/pom.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-reading</artifactId>
<groupId>com.xcs.spring</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-core-ioc</artifactId>
</project>

View File

@ -0,0 +1,19 @@
package com.xcs.spring;
import com.xcs.spring.config.MyConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author xcs
* @date 20230916 1609
**/
public class IOCApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
for (String beanDefinitionName : context.getBeanDefinitionNames()) {
System.out.println("Bean = " + context.getBean(beanDefinitionName));
}
context.close();
}
}

View File

@ -0,0 +1,13 @@
package com.xcs.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author xcs
* @date 20230919 1635
**/
@Configuration
@ComponentScan("com.xcs.spring.service")
public class MyConfiguration {
}

View File

@ -0,0 +1,15 @@
package com.xcs.spring.service;
import org.springframework.stereotype.Component;
/**
* @author xcs
* @date 20230921 1030
**/
@Component
public class MyServiceA {
public void destroy(){
System.out.println("MyServiceA.destroy");
}
}

View File

@ -0,0 +1,12 @@
package com.xcs.spring.service;
import org.springframework.stereotype.Component;
/**
* @author xcs
* @date 20230921 1030
**/
@Component
public class MyServiceB {
}

View File

@ -62,7 +62,7 @@ public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {
### 三、主要功能
+ 销毁前逻辑:使用 `postProcessBeforeDestruction(Object bean, String beanName)` 方法,我们可以为 bean 执行自定义的销毁逻辑。当一个 bean 被容器标记为销毁时,此方法将被调用。(例如,容器关闭时进行资源释放,状态记录,依赖清理)
+ **销毁前逻辑:**使用 `postProcessBeforeDestruction(Object bean, String beanName)` 方法,我们可以为 bean 执行自定义的销毁逻辑。当一个 bean 被容器标记为销毁时,此方法将被调用。(例如,容器关闭时进行资源释放,状态记录,依赖清理)
### 四、最佳实践