feat:增加SpringAOP文档

This commit is contained in:
2025-12-25 16:19:09 +08:00
parent 3911a00a51
commit 66d565d65b
5 changed files with 268 additions and 29 deletions

View File

@@ -38,6 +38,12 @@ public class AppConfig {
<bean id="userService" class="com.example.UserService"/>
```
::: tip
@Service@Controller这些注解是@Component的衍生注解
@Component注解可以通过@Autowired自动注入,一般用于自己编写的业务模块,需要配合@ComponentScan启用组件扫描(实际开发中@SpringBootApplication注解已经包含了@ComponentScan注解)。
@Bean注解一般用于注册第三方库的类
:::
## 2.4 作用域
&emsp;&emsp;控制 Bean 在容器中的实例数量和生命周期。Spring 提供多种作用域,默认是 singleton。通过 @Scope 注解指定,如 @Scope("prototype")。
- singleton单例容器中只有一个实例所有请求共享该实例默认值
@@ -46,25 +52,32 @@ public class AppConfig {
- sessionWeb每个会话创建一个实例在会话有效期内有效。
## 2.5 生命周期
1. 实例化Instantiation:容器通过构造器创建 Bean 实例(分配内存)。
2. 属性注入Population容器将依赖的 Bean 注入到当前 Bean 的字段或方法(如 @Autowired 标注的依赖)。
3. 初始化前Post-processing before initialization
执行 BeanPostProcessor 的 postProcessBeforeInitialization 方法AOP 代理生成在此阶段)。
4. 初始化Initialization
调用 @PostConstruct 注解的方法JSR-250 标准,推荐)。
若实现 InitializingBean 接口,调用 afterPropertiesSet() 方法。
执行自定义初始化方法(如 @Bean(initMethod = "init") 中指定的 init 方法)。
5. 初始化后Post-processing after initialization
执行 BeanPostProcessor 的 postProcessAfterInitialization 方法。
6. 使用In UseBean 被应用程序调用。
7. 销毁Destruction
调用 @PreDestroy 注解的方法JSR-250 标准,推荐)。
若实现 DisposableBean 接口,调用 destroy() 方法。
执行自定义销毁方法(如 @Bean(destroyMethod = "destroy") 中指定的 destroy 方法)。
1. 实例化Instantiation
容器通过构造器创建 Bean 实例(分配内存)。
2. 属性注入Population
容器将依赖的 Bean 注入到当前 Bean 的字段或方法(如 @Autowired 标注的依赖)。
3. 初始化前Post-processing before initialization
执行 BeanPostProcessor 的 postProcessBeforeInitialization 方法AOP 代理生成在此阶段)。
4. 初始化Initialization
调用 @PostConstruct 注解的方法JSR-250 标准,推荐)。
若实现 InitializingBean 接口,调用 afterPropertiesSet() 方法。
执行自定义初始化方法(如 @Bean(initMethod = "init") 中指定的 init 方法)。
5. 初始化后Post-processing after initialization
执行 BeanPostProcessor 的 postProcessAfterInitialization 方法。
6. 使用In Use
Bean 被应用程序调用。
7. 销毁Destruction
调用 @PreDestroy 注解的方法JSR-250 标准,推荐)。
若实现 DisposableBean 接口,调用 destroy() 方法。
执行自定义销毁方法(如 @Bean(destroyMethod = "destroy") 中指定的 destroy 方法)。
![暂无图片](https://cxx0822.iepose.cn/rustfs/blog/b1054386c885ffefb0f08074ad6b6268.png 'Bean生命周期.png')
# 三、BeanDefinition
## 3.1 定义
&emsp;&emsp;BeanDefinition是Spring框架中描述Bean的元数据对象它包含了创建Bean实例所需的所有信息。
@@ -172,7 +185,7 @@ public interface BeanFactory {
}
```
## 4.4 实现示例
## 4.4 使用示例
```java
// 创建BeanFactory实例
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
@@ -193,7 +206,7 @@ UserService userService = beanFactory.getBean("userService", UserService.class);
- 延迟加载默认在第一次请求时才创建Bean实例
- 基础功能提供最基本的IOC功能
# 五、ApplicationContext
# 五、ApplicationContext ★★★
## 5.1 定义
&emsp;&emsp;ApplicationContext是BeanFactory的子接口在BeanFactory的基础上提供了更多企业级功能是Spring容器的完整实现。
@@ -544,5 +557,5 @@ protected void finishRefresh() {
- 标记整个容器启动流程结束,容器进入就绪状态
### 5.4.13 总结
&emsp;&emsp;refresh()方法按固定顺序执行12个步骤核心可归纳为准备容器环境 → 解析并注册Bean定义→ 初始化事件、消息等基础设施 → 实例化所有非懒加载的单例Bean→ 完成启动并发布事件。整个过程通过模板方法模式定义,确保了扩展性和一致性。
&emsp;&emsp;核心在于两个关键扩展机制BeanFactoryPostProcessor在Bean定义阶段介入负责修改和注册Bean的定义信息BeanPostProcessor在Bean实例化阶段介入负责对创建好的Bean实例进行增强和包装。
&emsp;&emsp;refresh()方法按固定顺序执行12个步骤核心可归纳为**准备容器环境 → 解析并注册Bean定义→ 初始化事件、消息等基础设施 → 实例化所有非懒加载的单例Bean→ 完成启动并发布事件**。整个过程通过模板方法模式定义,确保了扩展性和一致性。
&emsp;&emsp;核心在于两个关键扩展机制:**BeanFactoryPostProcessor在Bean定义阶段介入负责修改和注册Bean的定义信息BeanPostProcessor在Bean实例化阶段介入负责对创建好的Bean实例进行增强和包装。**