InstantiationAwareBeanPostProcessor yes BeanPostProcessor Sub interface of , The role of this interface is perception Bean Instantiated processor .
In addition to two methods inherited from the parent interface , Three methods are also defined
method describe
postProcessBeforeInstantiation
Self method , Is the first method to execute , It is called before the target object is instantiated. , The return value of method to is Object type , We can return any type of value . Because the target object has not been instantiated at this time , So this return value can be used to replace the instance of the target object that was originally generated ( For example, proxy objects ). If the return value of the method replaces the target object that should have been generated , Only after that postProcessAfterInstantiation Method is called , Other methods are not called ; Otherwise, follow the normal process
postProcessAfterInstantiation
After the target object is instantiated, it is called. , At this point, the object has been instantiated , However, the properties of this instance have not been set , All of them null. Because its return value is to decide whether to call or not postProcessPropertyValues A factor in the method ( Because there is another factor mbd.getDependencyCheck()); If the method returns false, And it doesn't need to check, that postProcessPropertyValues Will be ignored and not executed ; If you return true,postProcessPropertyValues Will be executed
postProcessPropertyValues
Modify the property value , If postProcessAfterInstantiation Method return false, The method may not be called , The logarithmic property can be modified in this method
Create target class
public class User{ private int id; private String name; private String beanName
; public User() { System.out.println("User Instantiated ..."); } public int getId() {
return id; } public void setId(int id) { this.id = id; } public String getName()
{ return name; } public void setName(String name) { System.out.println(" set up :"+
name); this.name = name; } public String getBeanName() { return beanName; }
public void start() { System.out.println(" Custom initialization method ..."); } @Override public
StringtoString() { return "User [id=" + id + ", name=" + name + ", beanName=" +
beanName+ "]"; } }
Custom interface implementation class
public class MyInstantiationAwareBeanPostProcessor implements
InstantiationAwareBeanPostProcessor { @Override public Object
postProcessBeforeInitialization(Object bean, String beanName) throws
BeansException{ System.out.println(">>>postProcessBeforeInitialization"); return
bean; } @Override public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException { System.out.println(
">>>postProcessAfterInitialization"); return bean; } @Override public Object
postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws
BeansException{ System.out.println("-->postProcessBeforeInstantiation"); return
null; } @Override public boolean postProcessAfterInstantiation(Object bean,
String beanName) throws BeansException { System.out.println(
"-->postProcessAfterInstantiation"); return true; } @Override public
PropertyValuespostProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[]
pds, Object bean, String beanName) throws BeansException { System.out.println(
"<**postProcessPropertyValues**>"); return pvs; } }
test
public class Test { public static void main(String[] args) {
ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext(
"applicationContext.xml"); User user = ac.getBean(User.class); System.out.
println(user); ac.registerShutdownHook(); } }
If postProcessBeforeInstantiation Method returned Object yes null; So go straight back , call doCreateBean method ();
If postProcessBeforeInstantiation Return not for null; The instructions have been modified bean object ; And then it will be implemented immediately postProcessAfterInitialization method ( Note that this is the method after initialization , After instantiation through this method , After initialization, the method is executed directly ; After intermediate instantiation
and It is not executed before initialization );
Calling postProcessAfterInitialization If the null; So go straight back , call doCreateBean method ();( After initialization, the method returned null, You need to call doCreateBean Generated object )
Calling postProcessAfterInitialization Return not as null; So this one bean We'll go back to the ioc After the container is initialized, the operation is the last method ;
summary :
InstantiationAwareBeanPostProcessor Interface inheritance BeanPostProcessor Interface , It provides 3 Methods , Plus BeanPostProcessor Interface internal 2 Methods , So the implementation of this interface needs to be implemented 5 Methods .InstantiationAwareBeanPostProcessor The main function of the interface is the things that need to be handled in the instantiation process of the target object , It includes the pre and post process of instantiation object and the property setting of instance
postProcessBeforeInstantiation Method is the first method to execute , It is called before the target object is instantiated. , The return value type of this method is Object, We can return any type of value . Because the target object has not been instantiated at this time , So this return value can be used to replace the instance of the target object that should have been generated ( For example, proxy objects ). If the return value of the method replaces the target object that should have been generated , Only after that postProcessAfterInitialization Method is called , Other methods are no longer called ; Otherwise, follow the normal process
postProcessAfterInstantiation Method is called after the instantiation of the target object. , The object is instantiated at this time , However, the properties of this instance have not been set , All of them null. Because its return value is to decide whether to call or not postProcessPropertyValues One of the factors of the method ( Because there is another factor mbd.getDependencyCheck()); This method returns if false, And it doesn't need to check, that postProcessPropertyValues Will be ignored and not executed ; If you return true,
postProcessPropertyValues Will be executed
postProcessPropertyValues Method to modify the property value ( At this time, the property value has not been set , However, we can modify the property value that should be set in ). If postProcessAfterInstantiation Method return false, The method may not be called . Property values can be modified within this method
Parent interface BeanPostProcessor Of 2 Methods postProcessBeforeInitialization and postProcessAfterInitialization After the target object is instantiated , And the property is also called after it is set.
Technology