From 3ac810730d15c15971871ae68396ee1627684af4 Mon Sep 17 00:00:00 2001 From: xuchengsheng Date: Wed, 20 Sep 2023 15:07:56 +0800 Subject: [PATCH] =?UTF-8?q?MergedBeanDefinitionPostProcessor=E6=BA=90?= =?UTF-8?q?=E7=A0=81=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + pom.xml | 1 + .../src/main/java/com/xcs/spring/bean/A.java | 2 +- .../src/main/java/com/xcs/spring/bean/B.java | 2 +- .../main/java/com/xcs/spring/bean/MyBean.java | 2 +- .../com/xcs/spring/service/MyService1.java | 2 +- .../com/xcs/spring/service/MyService2.java | 2 +- .../java/com/xcs/spring/MyPackageMarker.java | 2 +- .../com/xcs/spring/component/MyComponent.java | 2 +- .../component/MyComponentAnnotation.java | 2 +- .../spring/component/MyComponentAspectJ.java | 2 +- .../spring/component/MyComponentCustom.java | 2 +- .../spring/component/MyComponentRegex.java | 2 +- .../com/xcs/spring/component/MyInterface.java | 2 +- .../config/MyCustomBeanNameGenerator.java | 2 +- .../config/MyCustomScopeMetadataResolver.java | 2 +- .../xcs/spring/controller/MyController1.java | 2 +- .../xcs/spring/controller/MyController2.java | 2 +- .../xcs/spring/filter/MyCustomTypeFilter.java | 2 +- .../com/xcs/spring/service/MyService1.java | 2 +- .../com/xcs/spring/service/MyService2.java | 2 +- .../main/java/com/xcs/spring/bean/MyBean.java | 2 +- .../java/com/xcs/spring/bean/MyBeanA.java | 2 +- .../java/com/xcs/spring/bean/MyBeanB.java | 2 +- .../java/com/xcs/spring/bean/MyBeanC.java | 2 +- .../java/com/xcs/spring/bean/MyBeanD.java | 2 +- .../main/java/com/xcs/spring/bean/User.java | 2 +- .../config/CustomDeferredImportSelector.java | 2 +- .../spring/config/CustomImportSelector.java | 2 +- .../xcs/spring/config/CustomRegistrar.java | 2 +- .../com/xcs/spring/config/UserConfig.java | 2 +- .../com/xcs/spring/service/MyService.java | 2 +- ...MyBeanDefinitionRegistryPostProcessor.java | 2 +- .../com/xcs/spring/config/MySimpleBean.java | 2 +- .../config/MyBeanFactoryPostProcessor.java | 2 +- .../com/xcs/spring/config/MySimpleBean.java | 2 +- spring-interface-beanPostProcessor/README.md | 43 +- .../spring/BeanPostProcessorApplication.java | 2 +- .../spring/config/MyBeanPostProcessor.java | 2 +- .../xcs/spring/config/MyConfiguration.java | 2 +- .../com/xcs/spring/service/MyService.java | 2 +- .../com/xcs/spring/service/MyServiceImpl.java | 2 +- ...tionAwareBeanPostProcessorApplication.java | 2 +- .../MyDestructionAwareBeanPostProcessor.java | 2 +- .../xcs/spring/service/ConnectionService.java | 2 +- .../spring/service/ConnectionServiceImpl.java | 2 +- ...tionAwareBeanPostProcessorApplication.java | 2 +- ...MyInstantiationAwareBeanPostProcessor.java | 2 +- .../java/com/xcs/spring/service/DataBase.java | 2 +- .../com/xcs/spring/service/DataBaseImpl.java | 2 +- .../README.md | 531 ++++++++++++++++++ .../pom.xml | 14 + ...eanDefinitionPostProcessorApplication.java | 18 + .../com/xcs/spring/annotation/MyValue.java | 16 + .../main/java/com/xcs/spring/bean/MyBean.java | 17 + .../xcs/spring/config/MyConfiguration.java | 24 + .../MyMergedBeanDefinitionPostProcessor.java | 57 ++ 57 files changed, 743 insertions(+), 74 deletions(-) create mode 100644 spring-interface-mergedBeanDefinitionPostProcessor/README.md create mode 100644 spring-interface-mergedBeanDefinitionPostProcessor/pom.xml create mode 100644 spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/MergedBeanDefinitionPostProcessorApplication.java create mode 100644 spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/annotation/MyValue.java create mode 100644 spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/bean/MyBean.java create mode 100644 spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/config/MyConfiguration.java create mode 100644 spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/config/MyMergedBeanDefinitionPostProcessor.java diff --git a/README.md b/README.md index 0e09c4c..ce3ff5e 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,5 @@ + [关于DestructionAwareBeanPostProcessor源码分析](spring-interface-destructionAwareBeanPostProcessor/README.md) ++ [关于MergedBeanDefinitionPostProcessor源码分析](spring-interface-mergedBeanDefinitionPostProcessor/README.md) + diff --git a/pom.xml b/pom.xml index 3146a97..7c63253 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,7 @@ spring-interface-beanDefinitionRegistryPostProcessor spring-interface-instantiationAwareBeanPostProcessor spring-interface-destructionAwareBeanPostProcessor + spring-interface-mergedBeanDefinitionPostProcessor diff --git a/spring-annotation-bean/src/main/java/com/xcs/spring/bean/A.java b/spring-annotation-bean/src/main/java/com/xcs/spring/bean/A.java index 32d132d..66a9f56 100644 --- a/spring-annotation-bean/src/main/java/com/xcs/spring/bean/A.java +++ b/spring-annotation-bean/src/main/java/com/xcs/spring/bean/A.java @@ -1,7 +1,7 @@ package com.xcs.spring.bean; /** - * @author 林雷 + * @author xcs * @date 2023年08月23日 15时30分 **/ public class A { diff --git a/spring-annotation-bean/src/main/java/com/xcs/spring/bean/B.java b/spring-annotation-bean/src/main/java/com/xcs/spring/bean/B.java index 896f265..b309663 100644 --- a/spring-annotation-bean/src/main/java/com/xcs/spring/bean/B.java +++ b/spring-annotation-bean/src/main/java/com/xcs/spring/bean/B.java @@ -1,7 +1,7 @@ package com.xcs.spring.bean; /** - * @author 林雷 + * @author xcs * @date 2023年08月23日 15时30分 **/ public class B { diff --git a/spring-annotation-bean/src/main/java/com/xcs/spring/bean/MyBean.java b/spring-annotation-bean/src/main/java/com/xcs/spring/bean/MyBean.java index 34a97c3..d72482c 100644 --- a/spring-annotation-bean/src/main/java/com/xcs/spring/bean/MyBean.java +++ b/spring-annotation-bean/src/main/java/com/xcs/spring/bean/MyBean.java @@ -1,7 +1,7 @@ package com.xcs.spring.bean; /** - * @author 林雷 + * @author xcs * @date 2023年08月25日 10时22分 **/ public class MyBean { diff --git a/spring-annotation-bean/src/main/java/com/xcs/spring/service/MyService1.java b/spring-annotation-bean/src/main/java/com/xcs/spring/service/MyService1.java index 7c45e8b..f7c43cb 100644 --- a/spring-annotation-bean/src/main/java/com/xcs/spring/service/MyService1.java +++ b/spring-annotation-bean/src/main/java/com/xcs/spring/service/MyService1.java @@ -3,7 +3,7 @@ package com.xcs.spring.service; import org.springframework.stereotype.Service; /** - * @author 林雷 + * @author xcs * @date 2023年08月14日 17时14分 **/ @Service diff --git a/spring-annotation-bean/src/main/java/com/xcs/spring/service/MyService2.java b/spring-annotation-bean/src/main/java/com/xcs/spring/service/MyService2.java index 299cf49..91a407e 100644 --- a/spring-annotation-bean/src/main/java/com/xcs/spring/service/MyService2.java +++ b/spring-annotation-bean/src/main/java/com/xcs/spring/service/MyService2.java @@ -3,7 +3,7 @@ package com.xcs.spring.service; import org.springframework.stereotype.Service; /** - * @author 林雷 + * @author xcs * @date 2023年08月14日 17时14分 **/ @Service diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/MyPackageMarker.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/MyPackageMarker.java index 23f451f..6657939 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/MyPackageMarker.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/MyPackageMarker.java @@ -1,7 +1,7 @@ package com.xcs.spring; /** - * @author 林雷 + * @author xcs * @date 2023年08月14日 15时06分 **/ public class MyPackageMarker { diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponent.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponent.java index f9602a1..3ea7784 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponent.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponent.java @@ -3,7 +3,7 @@ package com.xcs.spring.component; import org.springframework.stereotype.Component; /** - * @author 林雷 + * @author xcs * @date 2023年08月15日 14时13分 **/ @Component diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentAnnotation.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentAnnotation.java index b112333..e0791bd 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentAnnotation.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentAnnotation.java @@ -1,7 +1,7 @@ package com.xcs.spring.component; /** - * @author 林雷 + * @author xcs * @date 2023年08月16日 15时46分 **/ @MyAnnotation diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentAspectJ.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentAspectJ.java index 0ac1aff..615b424 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentAspectJ.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentAspectJ.java @@ -3,7 +3,7 @@ package com.xcs.spring.component; import org.springframework.stereotype.Component; /** - * @author 林雷 + * @author xcs * @date 2023年08月15日 15时04分 **/ @Component diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentCustom.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentCustom.java index f46eeb7..63d201a 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentCustom.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentCustom.java @@ -3,7 +3,7 @@ package com.xcs.spring.component; import org.springframework.stereotype.Component; /** - * @author 林雷 + * @author xcs * @date 2023年08月15日 14时15分 **/ @Component diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentRegex.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentRegex.java index da85a38..6e4097a 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentRegex.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyComponentRegex.java @@ -3,7 +3,7 @@ package com.xcs.spring.component; import org.springframework.stereotype.Component; /** - * @author 林雷 + * @author xcs * @date 2023年08月15日 14时59分 **/ @Component diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyInterface.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyInterface.java index bc0e6c9..d90b044 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyInterface.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/component/MyInterface.java @@ -1,7 +1,7 @@ package com.xcs.spring.component; /** - * @author 林雷 + * @author xcs * @date 2023年08月15日 14时12分 **/ public interface MyInterface { diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/config/MyCustomBeanNameGenerator.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/config/MyCustomBeanNameGenerator.java index ec2febb..a28ac86 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/config/MyCustomBeanNameGenerator.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/config/MyCustomBeanNameGenerator.java @@ -5,7 +5,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanNameGenerator; /** - * @author 林雷 + * @author xcs * @date 2023年08月14日 15时38分 **/ public class MyCustomBeanNameGenerator implements BeanNameGenerator { diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/config/MyCustomScopeMetadataResolver.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/config/MyCustomScopeMetadataResolver.java index 82809ce..484bc2b 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/config/MyCustomScopeMetadataResolver.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/config/MyCustomScopeMetadataResolver.java @@ -8,7 +8,7 @@ import org.springframework.context.annotation.ScopeMetadata; import org.springframework.context.annotation.ScopeMetadataResolver; /** - * @author 林雷 + * @author xcs * @date 2023年08月14日 17时07分 **/ public class MyCustomScopeMetadataResolver implements ScopeMetadataResolver { diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/controller/MyController1.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/controller/MyController1.java index 962dee4..0abb819 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/controller/MyController1.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/controller/MyController1.java @@ -3,7 +3,7 @@ package com.xcs.spring.controller; import org.springframework.stereotype.Controller; /** - * @author 林雷 + * @author xcs * @date 2023年08月14日 17时14分 **/ @Controller diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/controller/MyController2.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/controller/MyController2.java index 1cd89e2..5a172d9 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/controller/MyController2.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/controller/MyController2.java @@ -3,7 +3,7 @@ package com.xcs.spring.controller; import org.springframework.stereotype.Controller; /** - * @author 林雷 + * @author xcs * @date 2023年08月14日 17时14分 **/ @Controller diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/filter/MyCustomTypeFilter.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/filter/MyCustomTypeFilter.java index 82c5f2c..ccb3310 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/filter/MyCustomTypeFilter.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/filter/MyCustomTypeFilter.java @@ -7,7 +7,7 @@ import org.springframework.core.type.filter.TypeFilter; import java.io.IOException; /** - * @author 林雷 + * @author xcs * @date 2023年08月15日 14时15分 **/ public class MyCustomTypeFilter implements TypeFilter { diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/service/MyService1.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/service/MyService1.java index 7c45e8b..f7c43cb 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/service/MyService1.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/service/MyService1.java @@ -3,7 +3,7 @@ package com.xcs.spring.service; import org.springframework.stereotype.Service; /** - * @author 林雷 + * @author xcs * @date 2023年08月14日 17时14分 **/ @Service diff --git a/spring-annotation-componentScan/src/main/java/com/xcs/spring/service/MyService2.java b/spring-annotation-componentScan/src/main/java/com/xcs/spring/service/MyService2.java index 4a756dc..9cbf4a2 100644 --- a/spring-annotation-componentScan/src/main/java/com/xcs/spring/service/MyService2.java +++ b/spring-annotation-componentScan/src/main/java/com/xcs/spring/service/MyService2.java @@ -3,7 +3,7 @@ package com.xcs.spring.service; import org.springframework.stereotype.Service; /** - * @author 林雷 + * @author xcs * @date 2023年08月14日 17时14分 **/ @Service diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBean.java b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBean.java index 35dadb6..32228d3 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBean.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBean.java @@ -1,7 +1,7 @@ package com.xcs.spring.bean; /** - * @author 林雷 + * @author xcs * @date 2023年09月11日 11时13分 **/ public class MyBean { diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanA.java b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanA.java index cec752d..5605134 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanA.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanA.java @@ -1,7 +1,7 @@ package com.xcs.spring.bean; /** - * @author 林雷 + * @author xcs * @date 2023年08月28日 11时13分 **/ public class MyBeanA { diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanB.java b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanB.java index 373ba6f..f76fac8 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanB.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanB.java @@ -1,7 +1,7 @@ package com.xcs.spring.bean; /** - * @author 林雷 + * @author xcs * @date 2023年08月28日 11时13分 **/ public class MyBeanB { diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanC.java b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanC.java index 492bfde..53768b5 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanC.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanC.java @@ -1,7 +1,7 @@ package com.xcs.spring.bean; /** - * @author 林雷 + * @author xcs * @date 2023年08月28日 11时13分 **/ public class MyBeanC { diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanD.java b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanD.java index 73faec0..6de935e 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanD.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/bean/MyBeanD.java @@ -1,7 +1,7 @@ package com.xcs.spring.bean; /** - * @author 林雷 + * @author xcs * @date 2023年08月28日 11时13分 **/ public class MyBeanD { diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/bean/User.java b/spring-annotation-import/src/main/java/com/xcs/spring/bean/User.java index 1ffcdda..27d8a4b 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/bean/User.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/bean/User.java @@ -1,7 +1,7 @@ package com.xcs.spring.bean; /** - * @author 林雷 + * @author xcs * @date 2023年08月28日 10时48分 **/ public class User { diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomDeferredImportSelector.java b/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomDeferredImportSelector.java index 54c1665..4995b47 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomDeferredImportSelector.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomDeferredImportSelector.java @@ -6,7 +6,7 @@ import org.springframework.context.annotation.DeferredImportSelector; import org.springframework.core.type.AnnotationMetadata; /** - * @author 林雷 + * @author xcs * @date 2023年08月29日 11时08分 **/ public class CustomDeferredImportSelector implements DeferredImportSelector { diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomImportSelector.java b/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomImportSelector.java index c9fa087..b5c302b 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomImportSelector.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomImportSelector.java @@ -5,7 +5,7 @@ import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; /** - * @author 林雷 + * @author xcs * @date 2023年08月28日 11时12分 **/ public class CustomImportSelector implements ImportSelector { diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomRegistrar.java b/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomRegistrar.java index d99c893..d861b85 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomRegistrar.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/config/CustomRegistrar.java @@ -7,7 +7,7 @@ import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; /** - * @author 林雷 + * @author xcs * @date 2023年08月28日 11时17分 **/ public class CustomRegistrar implements ImportBeanDefinitionRegistrar { diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/config/UserConfig.java b/spring-annotation-import/src/main/java/com/xcs/spring/config/UserConfig.java index 23d89b1..22fa88d 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/config/UserConfig.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/config/UserConfig.java @@ -5,7 +5,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** - * @author 林雷 + * @author xcs * @date 2023年08月28日 10时48分 **/ @Configuration diff --git a/spring-annotation-import/src/main/java/com/xcs/spring/service/MyService.java b/spring-annotation-import/src/main/java/com/xcs/spring/service/MyService.java index c0dd7d7..bc0be61 100644 --- a/spring-annotation-import/src/main/java/com/xcs/spring/service/MyService.java +++ b/spring-annotation-import/src/main/java/com/xcs/spring/service/MyService.java @@ -4,7 +4,7 @@ import com.xcs.spring.bean.MyBeanD; import org.springframework.context.annotation.Bean; /** - * @author 林雷 + * @author xcs * @date 2023年08月28日 11时00分 **/ public class MyService { diff --git a/spring-interface-beanDefinitionRegistryPostProcessor/src/main/java/com/xcs/spring/config/MyBeanDefinitionRegistryPostProcessor.java b/spring-interface-beanDefinitionRegistryPostProcessor/src/main/java/com/xcs/spring/config/MyBeanDefinitionRegistryPostProcessor.java index d613fbc..c6c2fc3 100644 --- a/spring-interface-beanDefinitionRegistryPostProcessor/src/main/java/com/xcs/spring/config/MyBeanDefinitionRegistryPostProcessor.java +++ b/spring-interface-beanDefinitionRegistryPostProcessor/src/main/java/com/xcs/spring/config/MyBeanDefinitionRegistryPostProcessor.java @@ -8,7 +8,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProce import org.springframework.beans.factory.support.RootBeanDefinition; /** - * @author 林雷 + * @author xcs * @date 2023年09月14日 14时17分 **/ public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { diff --git a/spring-interface-beanDefinitionRegistryPostProcessor/src/main/java/com/xcs/spring/config/MySimpleBean.java b/spring-interface-beanDefinitionRegistryPostProcessor/src/main/java/com/xcs/spring/config/MySimpleBean.java index cfd9576..5c855f8 100644 --- a/spring-interface-beanDefinitionRegistryPostProcessor/src/main/java/com/xcs/spring/config/MySimpleBean.java +++ b/spring-interface-beanDefinitionRegistryPostProcessor/src/main/java/com/xcs/spring/config/MySimpleBean.java @@ -1,7 +1,7 @@ package com.xcs.spring.config; /** - * @author 林雷 + * @author xcs * @date 2023年09月14日 16时05分 **/ public class MySimpleBean { diff --git a/spring-interface-beanFactoryPostProcessor/src/main/java/com/xcs/spring/config/MyBeanFactoryPostProcessor.java b/spring-interface-beanFactoryPostProcessor/src/main/java/com/xcs/spring/config/MyBeanFactoryPostProcessor.java index 2b0cff7..3dba71a 100644 --- a/spring-interface-beanFactoryPostProcessor/src/main/java/com/xcs/spring/config/MyBeanFactoryPostProcessor.java +++ b/spring-interface-beanFactoryPostProcessor/src/main/java/com/xcs/spring/config/MyBeanFactoryPostProcessor.java @@ -6,7 +6,7 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; /** - * @author 林雷 + * @author xcs * @date 2023年09月14日 14时17分 **/ public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { diff --git a/spring-interface-beanFactoryPostProcessor/src/main/java/com/xcs/spring/config/MySimpleBean.java b/spring-interface-beanFactoryPostProcessor/src/main/java/com/xcs/spring/config/MySimpleBean.java index cfd9576..5c855f8 100644 --- a/spring-interface-beanFactoryPostProcessor/src/main/java/com/xcs/spring/config/MySimpleBean.java +++ b/spring-interface-beanFactoryPostProcessor/src/main/java/com/xcs/spring/config/MySimpleBean.java @@ -1,7 +1,7 @@ package com.xcs.spring.config; /** - * @author 林雷 + * @author xcs * @date 2023年09月14日 16时05分 **/ public class MySimpleBean { diff --git a/spring-interface-beanPostProcessor/README.md b/spring-interface-beanPostProcessor/README.md index b79f223..2ab8af3 100644 --- a/spring-interface-beanPostProcessor/README.md +++ b/spring-interface-beanPostProcessor/README.md @@ -418,7 +418,7 @@ public Object getSingleton(String beanName, ObjectFactory singletonFactory) { } ``` -在`org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean()`方法中,首先尝试调用`resolveBeforeInstantiation`,这个方法给`InstantiationAwareBeanPostProcessor`一个机会,允许它们返回一个代理对象,而不是目标bean的实例。如果这一步返回了一个非空的对象(也就是说,一个`InstantiationAwareBeanPostProcessor`创建了一个代理对象),那么这个代理对象将作为该bean的实例返回,跳过正常的bean创建过程。如果上面的步骤没有返回任何对象,那么代码将执行`doCreateBean`方法,这个方法负责实际的bean实例化、属性注入和初始化。 +在`org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean()`方法中,主要的逻辑是调用 `doCreateBean`,这是真正进行 bean 实例化、属性填充和初始化的地方。这个方法会返回新创建的 bean 实例。 ```java @Override @@ -530,7 +530,7 @@ public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, St } ``` -在`com.xcs.spring.config.MyBeanPostProcessor`方法中,将 `MyServiceImpl` 中的 `message` 属性,为其添加 `Prefix:` 前缀。在`postProcessAfterInitialization`方法中,将 `MyServiceImpl` 中的 `message` 属性,为其添加 `:Suffix` 后缀。 +最后执行到我们自定义的逻辑中,将 `MyServiceImpl` 中的 `message` 属性,为其添加 `Prefix:` 前缀。在`postProcessAfterInitialization`方法中,将 `MyServiceImpl` 中的 `message` 属性,为其添加 `:Suffix` 后缀。 ```java public class MyBeanPostProcessor implements BeanPostProcessor { @@ -596,39 +596,28 @@ public class MyBeanPostProcessor implements BeanPostProcessor { #### 8.2、源码分析总结 -**启动及初始化上下文**: +**Spring上下文初始化**: 通过`AnnotationConfigApplicationContext`的构造函数,Spring上下文会被初始化。这个过程中,`refresh()`方法是核心。 -- 使用`AnnotationConfigApplicationContext`进行Java配置方式的Spring上下文初始化。 -- 注册配置类,并刷新上下文,这触发了Spring容器的启动和bean生命周期的开始。 +**实例化单例Beans**: 在`refresh()`方法中,`finishBeanFactoryInitialization(beanFactory)`方法负责实例化所有剩余的非懒加载单例beans。 -**`refresh()`方法**: +**预实例化**: `preInstantiateSingletons()`方法负责预实例化所有的非懒加载的单例beans。对于每一个bean, 它都会调用`getBean`方法。 -- 是`AbstractApplicationContext`的核心方法,负责完整的容器刷新流程。 -- 重点关注的是`finishBeanFactoryInitialization(beanFactory)`方法,其负责初始化所有的非懒加载单例bean。 +**获取Bean**: `getBean()`方法负责获取bean实例,其核心是`doGetBean`方法,它会处理bean的实例化、初始化以及依赖注入。 -**预先实例化非懒加载的单例bean**: +**单例保证**: 在获取单例bean时, 如果bean已存在于单例缓存中,直接返回,否则会创建一个新的实例。 -- `preInstantiateSingletons()`方法循环遍历所有的bean定义,并为非懒加载的单例bean进行初始化。 -- 它对每个bean都调用了`getBean(beanName)`,从而触发了bean的创建过程。 +**Bean创建**: `createBean`方法调用`doCreateBean`来真正进行bean的实例化、属性填充和初始化。 -**`getBean()`方法**: +**属性填充与初始化**: `doCreateBean`方法有两个主要步骤: -- 是容器用于获取bean的核心方法。 -- 在内部,它调用了`doGetBean`,此方法处理bean的查找、创建和依赖注入。 +- `populateBean`: 属性注入 +- `initializeBean`: 执行bean的初始化方法,同时调用`BeanPostProcessor`的方法。 -**实际的bean创建过程**: +**BeanPostProcessors回调**: -- `doCreateBean`方法首先尝试用`InstantiationAwareBeanPostProcessor`创建代理对象(如果适用)。 -- 如果不返回代理对象,就会进入正常的bean创建、属性填充和初始化流程。 +- `initializeBean`会分别调用`applyBeanPostProcessorsBeforeInitialization`和`applyBeanPostProcessorsAfterInitialization`方法,分别在bean初始化前后进行处理。 +- 每个`BeanPostProcessor`都会参与上述的回调,允许对bean进行修改或增强。 -**`BeanPostProcessor`的角色**: +**自定义处理**: -- `BeanPostProcessor`接口是Spring中的核心扩展点,允许对bean实例进行额外的处理。 -- 在`initializeBean`方法中,我们可以看到`BeanPostProcessor`的两个主要回调方法——`postProcessBeforeInitialization`和`postProcessAfterInitialization`。 -- 这些方法提供了在bean初始化之前和之后进行额外处理的机会,如修改bean属性或返回代理对象。 - -**示例中的`MyBeanPostProcessor`**: - -- 我们定义的自定义`BeanPostProcessor`修改了`MyServiceImpl` bean的`message`属性。 -- 在初始化前,给`message`属性加了`Prefix:`前缀;在初始化后,又加了`:Suffix`后缀。 -- 最终的输出是对bean属性进行了修改的结果。 \ No newline at end of file +- 最后,我们的自定义`BeanPostProcessor` (`MyBeanPostProcessor`) 进行了特定的处理。在bean初始化前,为`message`添加了`Prefix:`,在bean初始化后,为`message`添加了`:Suffix`。 \ No newline at end of file diff --git a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/BeanPostProcessorApplication.java b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/BeanPostProcessorApplication.java index 6f4dc96..67486c4 100644 --- a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/BeanPostProcessorApplication.java +++ b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/BeanPostProcessorApplication.java @@ -5,7 +5,7 @@ import com.xcs.spring.service.MyService; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** - * @author 林雷 + * @author xcs * @date 2023年09月16日 16时09分 **/ public class BeanPostProcessorApplication { diff --git a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/config/MyBeanPostProcessor.java b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/config/MyBeanPostProcessor.java index 91777f0..03d6c32 100644 --- a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/config/MyBeanPostProcessor.java +++ b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/config/MyBeanPostProcessor.java @@ -5,7 +5,7 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** - * @author 林雷 + * @author xcs * @date 2023年09月19日 16时42分 **/ public class MyBeanPostProcessor implements BeanPostProcessor { diff --git a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/config/MyConfiguration.java b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/config/MyConfiguration.java index c2a5289..9600a7d 100644 --- a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/config/MyConfiguration.java +++ b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/config/MyConfiguration.java @@ -7,7 +7,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** - * @author 林雷 + * @author xcs * @date 2023年09月19日 16时35分 **/ @Configuration diff --git a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/service/MyService.java b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/service/MyService.java index 4f8a3c8..74eb99b 100644 --- a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/service/MyService.java +++ b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/service/MyService.java @@ -1,7 +1,7 @@ package com.xcs.spring.service; /** - * @author 林雷 + * @author xcs * @date 2023年09月19日 16时43分 **/ public interface MyService { diff --git a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/service/MyServiceImpl.java b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/service/MyServiceImpl.java index d6fa521..6ea2d3a 100644 --- a/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/service/MyServiceImpl.java +++ b/spring-interface-beanPostProcessor/src/main/java/com/xcs/spring/service/MyServiceImpl.java @@ -1,7 +1,7 @@ package com.xcs.spring.service; /** - * @author 林雷 + * @author xcs * @date 2023年09月19日 16时43分 **/ public class MyServiceImpl implements MyService{ diff --git a/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/DestructionAwareBeanPostProcessorApplication.java b/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/DestructionAwareBeanPostProcessorApplication.java index a2a4d1b..01c7095 100644 --- a/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/DestructionAwareBeanPostProcessorApplication.java +++ b/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/DestructionAwareBeanPostProcessorApplication.java @@ -5,7 +5,7 @@ import com.xcs.spring.service.ConnectionService; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** - * @author 林雷 + * @author xcs * @date 2023年09月16日 16时09分 **/ public class DestructionAwareBeanPostProcessorApplication { diff --git a/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/config/MyDestructionAwareBeanPostProcessor.java b/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/config/MyDestructionAwareBeanPostProcessor.java index 1c27769..74e0bde 100644 --- a/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/config/MyDestructionAwareBeanPostProcessor.java +++ b/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/config/MyDestructionAwareBeanPostProcessor.java @@ -5,7 +5,7 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; /** - * @author 林雷 + * @author xcs * @date 2023年09月18日 16时13分 **/ public class MyDestructionAwareBeanPostProcessor implements DestructionAwareBeanPostProcessor { diff --git a/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/ConnectionService.java b/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/ConnectionService.java index b31b285..619b3c1 100644 --- a/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/ConnectionService.java +++ b/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/ConnectionService.java @@ -1,7 +1,7 @@ package com.xcs.spring.service; /** - * @author 林雷 + * @author xcs * @date 2023年09月18日 17时29分 **/ public interface ConnectionService { diff --git a/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/ConnectionServiceImpl.java b/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/ConnectionServiceImpl.java index fb49c8b..f70074b 100644 --- a/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/ConnectionServiceImpl.java +++ b/spring-interface-destructionAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/ConnectionServiceImpl.java @@ -1,7 +1,7 @@ package com.xcs.spring.service; /** - * @author 林雷 + * @author xcs * @date 2023年09月18日 17时30分 **/ public class ConnectionServiceImpl implements ConnectionService { diff --git a/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/InstantiationAwareBeanPostProcessorApplication.java b/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/InstantiationAwareBeanPostProcessorApplication.java index 21fcf88..20d01cc 100644 --- a/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/InstantiationAwareBeanPostProcessorApplication.java +++ b/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/InstantiationAwareBeanPostProcessorApplication.java @@ -5,7 +5,7 @@ import com.xcs.spring.service.DataBase; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** - * @author 林雷 + * @author xcs * @date 2023年09月16日 16时09分 **/ public class InstantiationAwareBeanPostProcessorApplication { diff --git a/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/config/MyInstantiationAwareBeanPostProcessor.java b/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/config/MyInstantiationAwareBeanPostProcessor.java index 501a001..babd4a9 100644 --- a/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/config/MyInstantiationAwareBeanPostProcessor.java +++ b/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/config/MyInstantiationAwareBeanPostProcessor.java @@ -7,7 +7,7 @@ import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; /** - * @author 林雷 + * @author xcs * @date 2023年09月16日 16时12分 **/ public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor { diff --git a/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/DataBase.java b/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/DataBase.java index 8ae9f8b..d996670 100644 --- a/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/DataBase.java +++ b/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/DataBase.java @@ -1,7 +1,7 @@ package com.xcs.spring.service; /** - * @author 林雷 + * @author xcs * @date 2023年09月16日 16时10分 **/ public interface DataBase { diff --git a/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/DataBaseImpl.java b/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/DataBaseImpl.java index f194db0..b74cda6 100644 --- a/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/DataBaseImpl.java +++ b/spring-interface-instantiationAwareBeanPostProcessor/src/main/java/com/xcs/spring/service/DataBaseImpl.java @@ -4,7 +4,7 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; /** - * @author 林雷 + * @author xcs * @date 2023年09月16日 16时10分 **/ public class DataBaseImpl implements DataBase , InitializingBean { diff --git a/spring-interface-mergedBeanDefinitionPostProcessor/README.md b/spring-interface-mergedBeanDefinitionPostProcessor/README.md new file mode 100644 index 0000000..b73aa87 --- /dev/null +++ b/spring-interface-mergedBeanDefinitionPostProcessor/README.md @@ -0,0 +1,531 @@ +## MergedBeanDefinitionPostProcessor + +- [MergedBeanDefinitionPostProcessor](#mergedbeandefinitionpostprocessor) + - [一、接口描述](#一接口描述) + - [二、接口源码](#二接口源码) + - [三、主要功能](#三主要功能) + - [四、最佳实践](#四最佳实践) + - [五、时序图](#五时序图) + - [六、源码分析](#六源码分析) + - [七、注意事项](#七注意事项) + - [八、总结](#八总结) + - [8.1、最佳实践总结](#81最佳实践总结) + - [8.2、源码分析总结](#82源码分析总结) + +### 一、接口描述 + +`MergedBeanDefinitionPostProcessor` 是 Spring 框架中的一个接口,主要用于在 bean 定义被合并后(但在 bean 实例化之前)进行后处理。它扩展了 `BeanPostProcessor`,增加了处理合并 bean 定义的能力。 + +### 二、接口源码 + +`MergedBeanDefinitionPostProcessor` 是 Spring 框架自 2.5 版本开始引入的一个核心接口。其中的核心方法是`postProcessMergedBeanDefinition` 主要用途为提供了一个自定义或查询合并的 bean 定义的机会,例如应用自定义注释、修改 bean 元数据或基于合并的 bean 定义实现自定义行为。 + +```java +/** + * 用于在运行时后处理合并的 bean 定义的回调接口。 + * BeanPostProcessor 实现可以实现此子接口,以便在 Spring 的 BeanFactory + * 用于创建 bean 实例的时候对已合并的 bean 定义(原始 bean 定义的处理副本)进行后处理。 + * + * #postProcessMergedBeanDefinition 方法可以用于内省 bean 定义, + * 例如在后缀处理器 bean 的实例之前准备一些缓存的元数据。它也允许修改 bean 定义, + * 但仅限于那些实际上用于并发修改的定义属性。本质上,这只应用于 RootBeanDefinition + * 本身上定义的操作,但不包括其基类的属性。 + * + * @author Juergen Hoeller + * @since 2.5 + * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#getMergedBeanDefinition + */ +public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor { + + /** + * 后处理指定 bean 的给定合并 bean 定义。 + * @param beanDefinition bean 的合并定义 + * @param beanType 管理的 bean 实例的实际类型 + * @param beanName bean 的名称 + * @see AbstractAutowireCapableBeanFactory#applyMergedBeanDefinitionPostProcessors + */ + void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName); + + /** + * 通知指定名称的 bean 定义已被重置, + * 这个 post-processor 应该清除受影响的 bean 的任何元数据。 + * 默认实现是空的。 + * @param beanName bean 的名称 + * @since 5.1 + * @see DefaultListableBeanFactory#resetBeanDefinition + */ + default void resetBeanDefinition(String beanName) { + } +} +``` + +### 三、主要功能 + +**处理合并后的 Bean 定义**:在 Spring 中,一个 bean 可以继承另一个 bean 的配置,产生所谓的 "合并后的" bean 定义。这个合并的定义包括原始 bean 定义和任何父 bean 定义中的属性。`MergedBeanDefinitionPostProcessor` 允许开发者在 bean 的实例化和初始化之前,基于这个合并的定义执行定制逻辑。 + +**缓存元数据**:这个接口常常被用于检查 bean 定义并缓存相关的元数据,从而加速后续的 bean 实例化和初始化。例如,Spring 的 `AutowiredAnnotationBeanPostProcessor` 使用它来缓存 `@Autowired` 和 `@Value` 注解的信息。 + +**修改合并后的 Bean 定义**:虽然不是主要的使用场景,但 `MergedBeanDefinitionPostProcessor` 也允许修改合并后的 bean 定义。但这种修改应该小心进行,并且通常只限于那些真正用于并发修改的定义属性。 + +### 四、最佳实践 + +首先来看看启动类入口,上下文环境使用`AnnotationConfigApplicationContext`(此类是使用Java注解来配置Spring容器的方式),构造参数我们给定了一个`MyConfiguration`组件类。然后从Spring上下文中获取一个`MyBean`类型的bean,最后打印了该`getMessage`方法返回的值。 + +```java +public class MergedBeanDefinitionPostProcessorApplication { + + public static void main(String[] args) { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); + MyBean bean = context.getBean(MyBean.class); + System.out.println("message = " + bean.getMessage()); + } +} +``` + +这里使用`@Bean`注解,定义了两个Bean,是为了确保`MyBean`, `MergedBeanDefinitionPostProcessor` 被 Spring 容器执行 + +```java +@Configuration +public class MyConfiguration { + + @Bean + public static MergedBeanDefinitionPostProcessor myBeanPostProcessor() { + return new MyMergedBeanDefinitionPostProcessor(); + } + + @Bean + public MyBean myBean() { + return new MyBean(); + } +} +``` + +实现了 `MergedBeanDefinitionPostProcessor` 的类 `MyMergedBeanDefinitionPostProcessor`。这个类针对带有自定义注解 `MyValue` 的属性进行处理。在`postProcessMergedBeanDefinition`方法中,对于每个字段,检查是否有 `MyValue` 注解。如果有,则获取注解的值,并将字段和该值存储在 `defaultValues` 映射中。在`postProcessAfterInitialization`方法中,检查 `metadata` 是否包含这个 bean 的名称。如果包含,表示这个 bean 有需要处理的字段,还需检查该字段的当前值。如果字段的值为 null,则使用注解提供的值来设置该字段的值。 + +```java +public class MyMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor { + + /** + * 记录元数据 + */ + private Map> metadata = new HashMap<>(); + + @Override + public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) { + Field[] fields = beanType.getDeclaredFields(); + Map defaultValues = new HashMap<>(); + for (Field field : fields) { + if (field.isAnnotationPresent(MyValue.class)) { + MyValue annotation = field.getAnnotation(MyValue.class); + defaultValues.put(field, annotation.value()); + } + } + if (!defaultValues.isEmpty()) { + metadata.put(beanName, defaultValues); + } + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (metadata.containsKey(beanName)) { + Map defaultValues = metadata.get(beanName); + for (Map.Entry entry : defaultValues.entrySet()) { + Field field = entry.getKey(); + String value = entry.getValue(); + try { + field.setAccessible(true); + if (field.get(bean) == null) { + field.set(bean, value); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + return bean; + } +} +``` + +一个简单的注解类 + +```java +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface MyValue { + String value(); +} +``` + +一个简单的Bean类 + +```java +public class MyBean { + + @MyValue("hello world") + private String message; + + public String getMessage() { + return message; + } +} +``` + +运行结果发现,当 `MyBean` 实例化并初始化时,由于 `message` 字段的值未被明确设置,`MyMergedBeanDefinitionPostProcessor` 会使用 `MyValue` 注解中的默认值,即 "hello world",来为其赋值。 + +``` +message = hello world +``` + +### 五、时序图 + +~~~mermaid +sequenceDiagram + Title: MergedBeanDefinitionPostProcessor时序图 + participant MergedBeanDefinitionPostProcessorApplication + participant AnnotationConfigApplicationContext + participant AbstractApplicationContext + participant DefaultListableBeanFactory + participant AbstractBeanFactory + participant DefaultSingletonBeanRegistry + participant AbstractAutowireCapableBeanFactory + participant MyMergedBeanDefinitionPostProcessor + + MergedBeanDefinitionPostProcessorApplication->>AnnotationConfigApplicationContext:AnnotationConfigApplicationContext(componentClasses)
创建上下文 + AnnotationConfigApplicationContext->>AbstractApplicationContext:refresh()
刷新上下文 + AbstractApplicationContext->>AbstractApplicationContext:finishBeanFactoryInitialization(beanFactory)
初始化Bean工厂 + AbstractApplicationContext->>DefaultListableBeanFactory:preInstantiateSingletons()
实例化单例 + DefaultListableBeanFactory->>AbstractBeanFactory:getBean(name)
获取Bean + AbstractBeanFactory->>AbstractBeanFactory:doGetBean(name,requiredType,args,typeCheckOnly)
执行获取Bean + AbstractBeanFactory->>DefaultSingletonBeanRegistry:getSingleton(beanName,singletonFactory)
获取单例Bean + DefaultSingletonBeanRegistry-->>AbstractBeanFactory:getObject()
获取Bean实例 + AbstractBeanFactory->>AbstractAutowireCapableBeanFactory:createBean(beanName,mbd,args)
创建Bean + AbstractAutowireCapableBeanFactory->>AbstractAutowireCapableBeanFactory:doCreateBean(beanName,mbd,args)
执行Bean创建 + AbstractAutowireCapableBeanFactory->>AbstractAutowireCapableBeanFactory:applyMergedBeanDefinitionPostProcessors(mbd,beanType,beanName)
应用Post处理器 + AbstractAutowireCapableBeanFactory->>MyMergedBeanDefinitionPostProcessor:postProcessMergedBeanDefinition(beanDefinition,beanType,beanName)
处理合并定义 + AbstractAutowireCapableBeanFactory-->>AbstractBeanFactory:返回Bean对象 + AbstractBeanFactory-->>DefaultListableBeanFactory:返回Bean对象 + AnnotationConfigApplicationContext-->>MergedBeanDefinitionPostProcessorApplication:初始化完成 +~~~ + +### 六、源码分析 + +```java +public class MergedBeanDefinitionPostProcessorApplication { + + public static void main(String[] args) { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); + MyBean bean = context.getBean(MyBean.class); + System.out.println("message = " + bean.getMessage()); + } +} +``` + +首先我们来看看源码中的,构造函数中,执行了三个步骤,我们重点关注`refresh()`方法 + +```java +public AnnotationConfigApplicationContext(Class... componentClasses) { + this(); + register(componentClasses); + refresh(); +} +``` + +`org.springframework.context.support.AbstractApplicationContext#refresh`方法中我们重点关注一下`finishBeanFactoryInitialization(beanFactory)`这方法,其他方法不是本次源码阅读的重点暂时忽略,在`finishBeanFactoryInitialization(beanFactory)`方法会对实例化所有剩余非懒加载的单列Bean对象。 + +```java +@Override +public void refresh() throws BeansException, IllegalStateException { + // ... [代码部分省略以简化] + // Instantiate all remaining (non-lazy-init) singletons. + finishBeanFactoryInitialization(beanFactory); + // ... [代码部分省略以简化] +} +``` + +在`org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons`方法中,主要的核心目的是预先实例化所有非懒加载的单例bean。在Spring的上下文初始化完成后,该方法会被触发,以确保所有单例bean都被正确地创建并初始化。其中`getBean(beanName)`是此方法的核心操作。对于容器中定义的每一个单例bean,它都会调用`getBean`方法,这将触发bean的实例化、初始化及其依赖的注入。如果bean之前没有被创建过,那么这个调用会导致其被实例化和初始化。 + +```java +public void preInstantiateSingletons() throws BeansException { + // ... [代码部分省略以简化] + // 循环遍历所有bean的名称 + for (String beanName : beanNames) { + // 获取合并后的bean定义,这包括了从父容器继承的属性 + RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); + + // 检查bean是否不是抽象的、是否是单例的,以及是否不是懒加载的 + if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { + + // 判断当前bean是否是一个FactoryBean + if (isFactoryBean(beanName)) { + // 获取FactoryBean实例 + Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); + + // 如果bean确实是FactoryBean的实例 + if (bean instanceof FactoryBean) { + FactoryBean factory = (FactoryBean) bean; + + boolean isEagerInit; + + // 判断当前环境是否有安全管理器,并且工厂bean是否是SmartFactoryBean的实例 + if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { + // 使用AccessController确保在受限制的环境中安全地调用isEagerInit方法 + isEagerInit = AccessController.doPrivileged( + (PrivilegedAction) ((SmartFactoryBean) factory)::isEagerInit, + getAccessControlContext()); + } + else { + // 检查FactoryBean是否是SmartFactoryBean,并且是否需要立即初始化 + isEagerInit = (factory instanceof SmartFactoryBean && + ((SmartFactoryBean) factory).isEagerInit()); + } + + // 如果工厂bean需要立即初始化,则获取bean实例,这将触发bean的创建 + if (isEagerInit) { + getBean(beanName); + } + } + } + // 如果不是FactoryBean,则直接获取bean实例,这将触发bean的创建 + else { + getBean(beanName); + } + } + } + // ... [代码部分省略以简化] +} +``` + +在`org.springframework.beans.factory.support.AbstractBeanFactory#getBean()`方法中,又调用了`doGetBean`方法来实际执行创建Bean的过程,传递给它bean的名称和一些其他默认的参数值。此处,`doGetBean`负责大部分工作,如查找bean定义、创建bean(如果尚未创建)、处理依赖关系等。 + +```java +@Override +public Object getBean(String name) throws BeansException { + return doGetBean(name, null, null, false); +} +``` + +在`org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean`方法中,首先检查所请求的bean是否是一个单例并且已经创建。如果尚未创建,它将创建一个新的实例。在这个过程中,它处理可能的异常情况,如循环引用,并确保返回的bean是正确的类型。这是Spring容器bean生命周期管理的核心部分。 + +```java +protected T doGetBean( + String name, @Nullable Class requiredType, @Nullable Object[] args, boolean typeCheckOnly) + throws BeansException { + // ... [代码部分省略以简化] + + // 开始创建bean实例 + if (mbd.isSingleton()) { + // 如果bean是单例的,我们会尝试从单例缓存中获取它 + // 如果不存在,则使用lambda创建一个新的实例 + sharedInstance = getSingleton(beanName, () -> { + try { + // 尝试创建bean实例 + return createBean(beanName, mbd, args); + } + catch (BeansException ex) { + // 如果在创建bean过程中出现异常,从单例缓存中移除它 + // 这样做是为了防止循环引用的情况 + destroySingleton(beanName); + throw ex; + } + }); + // 对于某些bean(例如FactoryBeans),可能需要进一步处理以获取真正的bean实例 + beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); + } + // ... [代码部分省略以简化] + + // 确保返回的bean实例与请求的类型匹配 + return adaptBeanInstance(name, beanInstance, requiredType); +} +``` + +在`org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton()`方法中,主要负责从单例缓存中获取一个已存在的bean实例,或者使用提供的`ObjectFactory`创建一个新的实例。这是确保bean在Spring容器中作为单例存在的关键部分。 + +```java +public Object getSingleton(String beanName, ObjectFactory singletonFactory) { + // 断言bean名称不能为空 + Assert.notNull(beanName, "Bean name must not be null"); + + // 同步访问单例对象缓存,确保线程安全 + synchronized (this.singletonObjects) { + // 从缓存中获取单例对象 + Object singletonObject = this.singletonObjects.get(beanName); + + // 如果缓存中没有找到 + if (singletonObject == null) { + // ... [代码部分省略以简化] + + // 在创建单例之前执行某些操作,如记录创建状态 + beforeSingletonCreation(beanName); + + boolean newSingleton = false; + boolean recordSuppressedExceptions = (this.suppressedExceptions == null); + if (recordSuppressedExceptions) { + this.suppressedExceptions = new LinkedHashSet<>(); + } + + try { + // 使用工厂创建新的单例实例 + singletonObject = singletonFactory.getObject(); + newSingleton = true; + } + catch (IllegalStateException ex) { + // ... [代码部分省略以简化] + } + catch (BeanCreationException ex) { + // ... [代码部分省略以简化] + } + finally { + // ... [代码部分省略以简化] + } + + // 如果成功创建了新的单例,将其加入缓存 + if (newSingleton) { + addSingleton(beanName, singletonObject); + } + } + + // 返回单例对象 + return singletonObject; + } +} +``` + +在`org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean()`方法中,主要的逻辑是调用 `doCreateBean`,这是真正进行 bean 实例化、属性填充和初始化的地方。这个方法会返回新创建的 bean 实例。 + +```java +@Override +protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) + throws BeanCreationException { + + // ... [代码部分省略以简化] + + try { + // 正常的bean实例化、属性注入和初始化。 + // 这里是真正进行bean创建的部分。 + Object beanInstance = doCreateBean(beanName, mbdToUse, args); + // 记录bean成功创建的日志 + if (logger.isTraceEnabled()) { + logger.trace("Finished creating instance of bean '" + beanName + "'"); + } + return beanInstance; + } + catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) { + // ... [代码部分省略以简化] + } + catch (Throwable ex) { + // ... [代码部分省略以简化] + } +} +``` + +在`org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean`方法中,在 bean 的实例化前,会对合并的 bean 定义进行处理。这是 `MergedBeanDefinitionPostProcessors` 可以介入和修改合并后的 bean 定义的地方,为了确保对每个 bean 定义只执行一次后处理,有一个 `postProcessed` 标志,如果此标志为 `false`,则会调用 `applyMergedBeanDefinitionPostProcessors` 方法应用所有的 `MergedBeanDefinitionPostProcessors`,为了线程安全,此操作在 `mbd.postProcessingLock` 的同步块中执行。这确保了并发的 bean 创建请求不会导致对同一 bean 定义的重复后处理。 + +```java +protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) + throws BeanCreationException { + // ... [代码部分省略以简化] + // 允许MergedBeanDefinitionPostProcessor修改合并的bean定义 + synchronized (mbd.postProcessingLock) { + if (!mbd.postProcessed) { + try { + applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); + } + catch (Throwable ex) { + // ... [代码部分省略以简化] + } + mbd.postProcessed = true; + } + } + + // ... [代码部分省略以简化] + + // 返回创建和初始化后的bean + return exposedObject; +} +``` + +在`org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyMergedBeanDefinitionPostProcessors`方法中,遍历每一个 `MergedBeanDefinitionPostProcessor` 的 `postProcessMergedBeanDefinition` 方法,提供了一个自定义或查询合并的 bean 定义的机会。 + +```java +protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class beanType, String beanName) { + for (MergedBeanDefinitionPostProcessor processor : getBeanPostProcessorCache().mergedDefinition) { + processor.postProcessMergedBeanDefinition(mbd, beanType, beanName); + } +} +``` + +最后执行到我们自定义的逻辑中,对于每个字段,检查是否带有 `MyValue` 注解。如果带有,从该字段获取 `MyValue` 注解,并将字段与注解的值存储在 `defaultValues` `Map` 中,如果 `defaultValues` 不为空(即存在至少一个带有 `MyValue` 注解的字段),则将该 `Map` 存储在 `metadata` 中,键为 bean 的名称。 + +```java +public class MyMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor { + + /** + * 记录元数据 + */ + private Map> metadata = new HashMap<>(); + + @Override + public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) { + Field[] fields = beanType.getDeclaredFields(); + Map defaultValues = new HashMap<>(); + for (Field field : fields) { + if (field.isAnnotationPresent(MyValue.class)) { + MyValue annotation = field.getAnnotation(MyValue.class); + defaultValues.put(field, annotation.value()); + } + } + if (!defaultValues.isEmpty()) { + metadata.put(beanName, defaultValues); + } + } +} +``` + +### 七、注意事项 + +**调用时机**:`postProcessMergedBeanDefinition` 是在 bean 处于一个 "半实例化" 的状态。更确切地说,在此时,bean 的实例已经被创建,但属性注入、初始化方法等还没有执行,这意味着你不应该在此方法中尝试访问 bean 实例。 + +**避免修改不可变属性**:虽然我们可以在 `postProcessMergedBeanDefinition` 方法中修改 `RootBeanDefinition`,但应该小心只修改那些预期为可变的属性。例如(Bean的类名,构造函数参数值,懒加载标记,依赖信息,作用域,等等) + +**影响性能**:如果 `postProcessMergedBeanDefinition` 执行的操作很重,这可能会影响应用的启动性能,因为它会被每个 bean 的创建过程调用。 + +**防止无限递归**:如果你在 `postProcessMergedBeanDefinition` 方法中尝试获取其他 beans,这可能会触发那些 beans 的创建,从而再次调用 `postProcessMergedBeanDefinition`。你应该注意避免这种无限递归的情况。 + +### 八、总结 + +#### 8.1、最佳实践总结 + +**启动类入口**:`MergedBeanDefinitionPostProcessorApplication` 是应用的主入口。在这里,我们使用了 `AnnotationConfigApplicationContext` 来初始化和配置 Spring 容器,并为其提供了一个配置类 `MyConfiguration`。 + +**配置类**:在 `MyConfiguration` 中,我们定义了两个bean:一个是自定义的 `MyMergedBeanDefinitionPostProcessor`,另一个是一个简单的 `MyBean` 类实例。 + +**自定义后处理器**:`MyMergedBeanDefinitionPostProcessor` 实现了 `MergedBeanDefinitionPostProcessor` 接口,允许我们在bean的实例化之前处理和修改其定义。在这个示例中,我们检查bean的字段是否有 `MyValue` 注解。如果有,我们将字段的名称和注解的值存储在一个映射中。然后,在bean的实例化和初始化后,我们检查是否需要为字段设置值。如果字段的当前值是 `null`,我们使用 `MyValue` 注解提供的值来设置它。 + +**自定义注解**:`MyValue` 是一个简单的自定义注解,用于指定一个字段的默认值。 + +**目标Bean**:`MyBean` 是一个简单的Java类,其中一个字段 `message` 被标记为 `MyValue("hello world")`。这意味着,如果在Spring容器初始化此bean时,`message` 字段没有被明确设置一个值,那么它将使用 `MyValue` 注解中的默认值 "hello world"。 + +**执行结果**:当应用程序运行时,Spring容器会实例化并初始化 `MyBean`。由于 `message` 字段的值未被明确设置,因此 `MyMergedBeanDefinitionPostProcessor` 将使用 `MyValue` 注解中的默认值 "hello world" 为其赋值。最后,应用程序输出 "message = hello world"。 + +#### 8.2、源码分析总结 + +**启动类**:应用的主入口是`MergedBeanDefinitionPostProcessorApplication`。它使用`AnnotationConfigApplicationContext`来初始化Spring容器,并传入配置类`MyConfiguration`。 + +**初始化Spring容器**:在`AnnotationConfigApplicationContext`的构造函数中,除了一些基本的配置外,它主要调用了`refresh()`方法来完成容器的初始化。 + +**容器刷新**:`refresh()`方法是在`AbstractApplicationContext`中定义的,用于完成容器的初始化。其中,`finishBeanFactoryInitialization(beanFactory)`方法被用来实例化所有非懒加载的单例Bean对象。 + +**实例化单例Beans**:`preInstantiateSingletons()`方法在`DefaultListableBeanFactory`中被调用,用于预先实例化所有非懒加载的单例bean。该方法通过调用`getBean(beanName)`来实例化和初始化bean。 + +**Bean获取**:`getBean()`方法在`AbstractBeanFactory`中定义,它最终会调用`doGetBean()`方法来完成实际的Bean创建工作。 + +**Bean的创建**:`doGetBean()`方法处理bean的查找、创建和依赖处理。如果请求的bean是一个单例并且尚未创建,则它将使用`getSingleton()`方法从单例缓存中获取或创建新的实例。 + +**处理单例Beans**:在`DefaultSingletonBeanRegistry`中,`getSingleton()`方法用于从单例缓存中获取已存在的bean或使用`ObjectFactory`创建新的实例。 + +**实际Bean的创建**:在`AbstractAutowireCapableBeanFactory`中,`createBean()`方法是Bean创建的入口,它主要调用`doCreateBean()`方法。在`doCreateBean()`中,`applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName)`方法允许`MergedBeanDefinitionPostProcessors`修改合并的bean定义。 + +**应用合并的Bean定义后处理器**:`applyMergedBeanDefinitionPostProcessors()`方法遍历并调用每个`MergedBeanDefinitionPostProcessor`的`postProcessMergedBeanDefinition()`方法。这为每个合并的Bean定义提供了自定义或查询的机会。 + +**自定义后处理器逻辑**:在我们的例子中,`MyMergedBeanDefinitionPostProcessor`对带有`MyValue`注解的属性进行了处理。它在`postProcessMergedBeanDefinition()`中检查每个字段是否有`MyValue`注解,并为这些字段收集默认值。 \ No newline at end of file diff --git a/spring-interface-mergedBeanDefinitionPostProcessor/pom.xml b/spring-interface-mergedBeanDefinitionPostProcessor/pom.xml new file mode 100644 index 0000000..7869a8c --- /dev/null +++ b/spring-interface-mergedBeanDefinitionPostProcessor/pom.xml @@ -0,0 +1,14 @@ + + + + spring-reading + com.xcs.spring + 0.0.1-SNAPSHOT + + 4.0.0 + + spring-interface-mergedBeanDefinitionPostProcessor + + \ No newline at end of file diff --git a/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/MergedBeanDefinitionPostProcessorApplication.java b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/MergedBeanDefinitionPostProcessorApplication.java new file mode 100644 index 0000000..36ff96a --- /dev/null +++ b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/MergedBeanDefinitionPostProcessorApplication.java @@ -0,0 +1,18 @@ +package com.xcs.spring; + +import com.xcs.spring.bean.MyBean; +import com.xcs.spring.config.MyConfiguration; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * @author xcs + * @date 2023年09月16日 16时09分 + **/ +public class MergedBeanDefinitionPostProcessorApplication { + + public static void main(String[] args) { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); + MyBean bean = context.getBean(MyBean.class); + System.out.println("message = " + bean.getMessage()); + } +} diff --git a/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/annotation/MyValue.java b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/annotation/MyValue.java new file mode 100644 index 0000000..7a6805d --- /dev/null +++ b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/annotation/MyValue.java @@ -0,0 +1,16 @@ +package com.xcs.spring.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author xcs + * @date 2023年09月20日 10时35分 + **/ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface MyValue { + String value(); +} diff --git a/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/bean/MyBean.java b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/bean/MyBean.java new file mode 100644 index 0000000..9db1660 --- /dev/null +++ b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/bean/MyBean.java @@ -0,0 +1,17 @@ +package com.xcs.spring.bean; + +import com.xcs.spring.annotation.MyValue; + +/** + * @author 林雷 + * @date 2023年09月20日 10时52分 + **/ +public class MyBean { + + @MyValue("hello world") + private String message; + + public String getMessage() { + return message; + } +} diff --git a/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/config/MyConfiguration.java b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/config/MyConfiguration.java new file mode 100644 index 0000000..3c1d177 --- /dev/null +++ b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/config/MyConfiguration.java @@ -0,0 +1,24 @@ +package com.xcs.spring.config; + +import com.xcs.spring.bean.MyBean; +import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author xcs + * @date 2023年09月19日 16时35分 + **/ +@Configuration +public class MyConfiguration { + + @Bean + public static MergedBeanDefinitionPostProcessor myBeanPostProcessor() { + return new MyMergedBeanDefinitionPostProcessor(); + } + + @Bean + public MyBean myBean() { + return new MyBean(); + } +} diff --git a/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/config/MyMergedBeanDefinitionPostProcessor.java b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/config/MyMergedBeanDefinitionPostProcessor.java new file mode 100644 index 0000000..aee8caf --- /dev/null +++ b/spring-interface-mergedBeanDefinitionPostProcessor/src/main/java/com/xcs/spring/config/MyMergedBeanDefinitionPostProcessor.java @@ -0,0 +1,57 @@ +package com.xcs.spring.config; + +import com.xcs.spring.annotation.MyValue; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; +import org.springframework.beans.factory.support.RootBeanDefinition; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +/** + * @author xcs + * @date 2023年09月19日 16时42分 + **/ +public class MyMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor { + + /** + * 记录元数据 + */ + private Map> metadata = new HashMap<>(); + + @Override + public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) { + Field[] fields = beanType.getDeclaredFields(); + Map defaultValues = new HashMap<>(); + for (Field field : fields) { + if (field.isAnnotationPresent(MyValue.class)) { + MyValue annotation = field.getAnnotation(MyValue.class); + defaultValues.put(field, annotation.value()); + } + } + if (!defaultValues.isEmpty()) { + metadata.put(beanName, defaultValues); + } + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (metadata.containsKey(beanName)) { + Map defaultValues = metadata.get(beanName); + for (Map.Entry entry : defaultValues.entrySet()) { + Field field = entry.getKey(); + String value = entry.getValue(); + try { + field.setAccessible(true); + if (field.get(bean) == null) { + field.set(bean, value); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + return bean; + } +}