File tree Expand file tree Collapse file tree 4 files changed +86
-0
lines changed
project/spring-source/spring-demo1/src/main/java/com/imooc/c_refresh Expand file tree Collapse file tree 4 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 11package com .imooc .c_refresh ;
22
3+ import com .imooc .b_ioc_learn .User ;
4+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
5+ import org .springframework .context .annotation .ComponentScan ;
6+ import org .springframework .context .annotation .Configuration ;
7+
8+ @ Configuration
9+ @ ComponentScan ("com.imooc.c_refresh" )
310public class A1_PostProcessor {
411
512/**
@@ -14,5 +21,10 @@ public class A1_PostProcessor {
1421 BeanPostProcessor
1522
1623 */
24+ public static void main (String [] args ) {
25+ AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext (A1_PostProcessor .class );
26+ User user5 = (User )app .getBean ("user5" );
27+ System .out .println ("##########" +user5 );
28+ }
1729
1830}
Original file line number Diff line number Diff line change 1+ package com .imooc .c_refresh ;
2+
3+ import com .imooc .b_ioc_learn .User ;
4+ import org .springframework .beans .BeansException ;
5+ import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
6+ import org .springframework .beans .factory .support .*;
7+ import org .springframework .context .annotation .Configuration ;
8+
9+ @ Configuration
10+ public class A2_CustomizedBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
11+
12+ /**
13+ * BeanDefinitionRegistryPostProcessor
14+ * 允许在正常的BeanFactoryPostProcessor检测开始之前注册更多的自定义beandefinition
15+ */
16+
17+ @ Override
18+ public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry ) throws BeansException {
19+ BeanDefinitionBuilder builder = BeanDefinitionBuilder .genericBeanDefinition (User .class );
20+ GenericBeanDefinition definition = (GenericBeanDefinition )builder .getRawBeanDefinition ();
21+ registry .registerBeanDefinition ("user5" , definition );
22+
23+ }
24+
25+ @ Override
26+ public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory ) throws BeansException {
27+
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .imooc .c_refresh ;
2+
3+ import org .springframework .beans .BeansException ;
4+ import org .springframework .beans .factory .config .BeanPostProcessor ;
5+ import org .springframework .context .annotation .Configuration ;
6+
7+ @ Configuration
8+ public class B1_CustomizedBeanPostProcessor implements BeanPostProcessor {
9+
10+ /**
11+ * 每个Bean注册后,调用后置处理器前执行此方法
12+ */
13+ @ Override
14+ public Object postProcessBeforeInitialization (Object bean , String beanName ) throws BeansException {
15+ System .out .println ("########postProcessBeforeInitialization##" +beanName );
16+ return bean ;
17+ }
18+
19+ /**
20+ * 每个Bean注册后,调用后置处理器后执行此方法
21+ */
22+ @ Override
23+ public Object postProcessAfterInitialization (Object bean , String beanName ) throws BeansException {
24+ System .out .println ("########postProcessAfterInitialization##" +beanName );
25+ return bean ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package com .imooc .c_refresh ;
2+
3+ import com .imooc .b_ioc_learn .User ;
4+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
5+ import org .springframework .context .annotation .ComponentScan ;
6+ import org .springframework .context .annotation .Configuration ;
7+
8+ @ Configuration
9+ @ ComponentScan ("com.imooc" )
10+ public class B1_CustomizedBeanPostProcessor_Learn {
11+
12+ public static void main (String [] args ) {
13+ AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext (A1_PostProcessor .class );
14+ User user5 = (User )app .getBean ("user5" );
15+ System .out .println ("##########" +user5 );
16+ }
17+
18+ }
You can’t perform that action at this time.
0 commit comments