Skip to content

Commit 4de3a91

Browse files
committed
00
1 parent 44e8037 commit 4de3a91

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

project/spring-source/spring-demo1/src/main/java/com/imooc/c_refresh/A1_PostProcessor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package 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")
310
public 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 numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

0 commit comments

Comments
 (0)