diff --git a/spring-aop/spring-aop-targetSource/README.md b/spring-aop/spring-aop-targetSource/README.md index a5f1852..1f621ab 100644 --- a/spring-aop/spring-aop-targetSource/README.md +++ b/spring-aop/spring-aop-targetSource/README.md @@ -1,15 +1,13 @@ ## TargetSource -- [TargetSource](#TargetSource) +- [TargetSource](#targetsource) - [一、基本信息](#一基本信息) - - [二、知识储备](#二知识储备) - - [三、基本描述](#三基本描述) - - [四、主要功能](#四主要功能) - - [五、接口源码](#五接口源码) - - [六、主要实现](#六主要实现) + - [二、基本描述](#二基本描述) + - [三、主要功能](#三主要功能) + - [四、接口源码](#四接口源码) + - [五、主要实现](#五主要实现) + - [六、类关系图](#六类关系图) - [七、最佳实践](#七最佳实践) - - [八、与其他组件的关系](#八与其他组件的关系) - - [九、常见问题](#九常见问题) ### 一、基本信息 @@ -115,7 +113,32 @@ public interface TargetSource extends TargetClassAware { + 用于使用 Apache Commons Pool 来管理目标对象的池化目标源。该实现通过对象池管理目标对象的创建和销毁,以提高对象的重用性和性能。 -### 六、最佳实践 +### 六、类关系图 + +~~~mermaid +classDiagram +direction BT +class CommonsPool2TargetSource +class PrototypeTargetSource +class SingletonTargetSource +class TargetClassAware { +<> + +} +class TargetSource { +<> + +} +class ThreadLocalTargetSource + +CommonsPool2TargetSource ..> TargetSource +PrototypeTargetSource ..> TargetSource +SingletonTargetSource ..> TargetSource +TargetSource --> TargetClassAware +ThreadLocalTargetSource ..> TargetSource +~~~ + +### 七、最佳实践 使用 Spring 的代理工厂(`ProxyFactory`)和目标源(`TargetSource`)来创建代理对象。在这个示例中,我们创建了一个连接池目标源(`ConnectionPoolTargetSource`),设置连接池的大小为 3。然后,我们将这个连接池目标源设置为代理工厂的目标源,并通过代理工厂获取代理对象。最后,我们通过代理对象调用了10次方法。 @@ -264,17 +287,3 @@ MyConnection Name = Connection1 MyConnection Name = Connection2 MyConnection Name = Connection0 ``` - -### 七、源码分析 - -暂时 - -### 八、常见问题 - -1. **目标对象类型不匹配** - - + 在配置 `TargetSource` 实现类时,需要确保目标对象的类型与实际目标对象的类型一致,否则可能会导致类型转换异常或者运行时错误。 - -2. **目标对象生命周期管理** - - + 在使用 `TargetSource` 时,需要确保目标对象的生命周期管理正确,特别是在目标对象的初始化、销毁和异常处理方面,需要注意避免资源泄漏和对象状态不一致等问题。 \ No newline at end of file diff --git a/spring-aop/spring-aop-targetSourceCreator/README.md b/spring-aop/spring-aop-targetSourceCreator/README.md index 7a32f39..2d14f5b 100644 --- a/spring-aop/spring-aop-targetSourceCreator/README.md +++ b/spring-aop/spring-aop-targetSourceCreator/README.md @@ -1,14 +1,14 @@ ## TargetSourceCreator -- [TargetSourceCreator](#TargetSourceCreator) +- [TargetSourceCreator](#targetsourcecreator) - [一、基本信息](#一基本信息) - [二、基本描述](#二基本描述) - [三、主要功能](#三主要功能) - [四、接口源码](#四接口源码) - [五、主要实现](#五主要实现) - - [六、最佳实践](#六最佳实践) - - [七、源码分析](#七源码分析) - - [八、常见问题](#八常见问题) + - [六、类关系图](#六类关系图) + - [七、最佳实践](#七最佳实践) + - [八、源码分析](#八源码分析) ### 一、基本信息 @@ -70,7 +70,25 @@ public interface TargetSourceCreator { + 用于延迟创建目标源。它适用于需要延迟加载的场景,以减少启动时间或资源占用。根据特定的条件或策略,它会延迟地创建目标源,直到被请求时才进行加载。 -### 六、最佳实践 +### 六、类关系图 + +~~~mermaid +classDiagram +direction BT +class AbstractBeanFactoryBasedTargetSourceCreator +class LazyInitTargetSourceCreator +class QuickTargetSourceCreator +class TargetSourceCreator { +<> + +} + +AbstractBeanFactoryBasedTargetSourceCreator ..> TargetSourceCreator +LazyInitTargetSourceCreator --> AbstractBeanFactoryBasedTargetSourceCreator +QuickTargetSourceCreator --> AbstractBeanFactoryBasedTargetSourceCreator +~~~ + +### 七、最佳实践 使用Spring框架中的注解配置来创建应用程序上下文,并从上下文中获取`MyConnection` bean。然后,它打印了`MyConnection`实例的类名,并循环调用了`MyConnection`实例的`getName()`方法来获取实例的名称并打印输出。 @@ -266,7 +284,7 @@ MyConnection Name = Connection2 MyConnection Name = Connection0 ``` -### 七、源码分析 +### 八、源码分析 在`org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessBeforeInstantiation`方法中,在Bean实例化之前进行处理。首先,它检查缓存中是否存在目标Bean的信息,如果存在则直接返回null,否则继续执行。然后,它检查Bean是否是基础设施类或是否应该被跳过,如果是,则将其标记为不需要增强,并返回null。最后,如果存在自定义的目标源(TargetSource),则创建代理对象,并使用自定义的目标源处理目标实例,从而避免不必要的默认实例化过程。 @@ -348,9 +366,3 @@ protected TargetSource getCustomTargetSource(Class beanClass, String beanName return null; } ``` - -### 八、常见问题 - -1. **如何在Spring应用程序中配置和使用TargetSourceCreator?** - - - 配置`TargetSourceCreator`,创建一个实现`BeanPostProcessor`和`PriorityOrdered`接口的自定义类`SetMyTargetSourceCreator`,在其`postProcessAfterInitialization`方法中设置自定义的目标源创建器。 \ No newline at end of file