forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInstanceBeanPostProcessor.java
66 lines (57 loc) · 2.73 KB
/
InstanceBeanPostProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.debug.beanlifecycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import java.beans.PropertyDescriptor;
import java.util.Arrays;
/**
* @author: Shawn Chen
* @date: 2018/6/6
* @description:自定义InstantiationAwareBeanPostProcessor类
*/
public class InstanceBeanPostProcessor implements InstantiationAwareBeanPostProcessor
{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
{
System.out.println("10.调用InstantiationAwareBeanPostProcessor中的postProcessBeforeInitialization()方法, beanName = " + beanName);
System.out.println();
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
{
System.out.println("14.执行InstantiationAwareBeanPostProcessor的postProcessAfterInitialization()方法");
System.out.println();
return bean;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException
{
System.out.println("4.调用InstantiationAwareBeanPostProcessor中的postProcessAfterInstantiation()方法");
System.out.println();
System.out.println("4.返回boolean,bean实例化后调用,并且返回false则不会注入属性,beanName" + beanName);
System.out.println();
return true;
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException
{
System.out.println("2.调用InstantiationAwareBeanPostProcessor中的postProcessBeforeInstantiation()方法");
System.out.println();
System.out.println("2.实例化bean之前调用,即调用bean类构造函数之前调用 " + beanClass.getName());
System.out.println();
return null;
}
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException
{
System.out.println("5.调用InstantiationAwareBeanPostProcessor中的postProcessPropertyValues()方法");
System.out.println();
System.out.println("5.postProcessPropertyValues,在属性注入之前调用...... beanName = " + beanName + " 属性名集合 : " + Arrays.toString(pvs.getPropertyValues()));
System.out.println();
System.out.println("这里Bean的name属性还是null:" + ((BeanLifeCycle) bean).getName()); //这里可以看到nam的值
System.out.println();
return pvs;//这里要返回propertyValues,否则属性无法注入
}
}