What exactly is the bean's life cycle like in spring?
1. The container looks up the bean's definition information and instantiates it
2. Using dependency injection, spring configures all the Bean's properties according to the Bean definition information
3. If the bean implements the Beannameaware interface, the factory calls the Bean's Setbeanname () method to pass the Bean's ID
4. If the bean implements the Beanfactoryaware interface, the factory calls the Setbeanfactory () method to pass in the factory itself
5. If beanpostprocessor is associated with a bean, its postprocessbeforeinitialization () method is called
6. If the bean specifies the Init-method method, it will be called
7. Finally, if a beanpostprocessor is associated with a bean, its postprocessafterinitialization () method is called
At this point, the bean can already be used by the application system and will be kept in beanfactory to know that he is no longer needed. There are two ways to remove it from beanfactory
① If the bean implements the Disposablebean interface, the Destroy () method is called
② This method is called if a custom destroy method is specified
public class Myinstantiationawarebeanpostprocessor extends instantiationawarebeanpostprocessoradapter{
Before construct
@Override public Object postprocessbeforeinstantiation (class<?> beanclass, String beanname) throws beansexception {
System.out.println ("Before Beanclass:" + beanclass.getname () + beanname);
return null;
}
After construct
@Override public boolean postprocessafterinstantiation (Object bean, String beanname) throws Beansexception {
System.out.println ("After Beanclass:" + bean + beanname);
return true;
}
Before set filled
@Override public propertyvalues postprocesspropertyvalues (propertyvalues PVs, propertydescriptor[] pds, Object Bean,
String beanname) throws Beansexception {
System.out.println ("Postprocesspropertyvalues");
Return Super.postprocesspropertyvalues (PVS, PDS, Bean, beanname);
}
}
public class Mybeanpostprocessor implements Beanpostprocessor {
Before Init
@Override public Object Postprocessbeforeinitialization (object bean, String beanname) throws Beansexception {
System.out.println ("Postprocessbeforeinitialization");
return bean;
}
After Init
@Override public Object Postprocessafterinitialization (object bean, String beanname) throws Beansexception {
System.out.println ("Postprocessafterinitialization");
return bean;
}
}
public class Car implements Beanfactoryaware, Beannameaware, Initializingbean, Disposablebean {
Private String brand;
private String color;
private int maxspeed;
Private String Beanname;
Public String Getbrand () {
return brand;
}
Public Car () {
System.out.println ("construct");
}
public void Setbrand (String brand) {
System.out.println ("set");
This.brand = brand;
}
Public String GetColor () {
return color;
}
public void SetColor (String color) {
This.color = color;
}
public int getmaxspeed () {
return maxspeed;
}
public void setmaxspeed (int maxspeed) {
This.maxspeed = Maxspeed;
}
@Override
Public String toString () {
Return "car{" + "brand=" "+ Brand +" \ "+", color= ' + color + ' \ ' + ", maxspeed=" + Maxspeed + '} ';
}
After set Setbeanname
@Override
public void Setbeanfactory (Beanfactory beanfactory) throws Beansexception {
System.out.println ("Beanfactoryaware:" + Beanfactory.containsbean (beanname));
}
After set filed
@Override
public void Setbeanname (String name) {
System.out.println ("Setbeanname:" + name);
This.beanname = name;
}
@Override
public void Afterpropertiesset () throws Exception {
System.out.println ("After properties set");
}
@Override public void Destroy () throws Exception {
System.out.println ("Destory");
}
Init
public void init () {
System.out.println ("Init");
}
public void Destory () {
System.out.println ("Destory");
}
}
Load class to Beanfactory,bean life cycle
@Test
public void Test3 () {
String Path = Demo1.class.getResource ("/"). GetPath (). Concat ("Beans.xml");
Filesystemresource resource = new Filesystemresource (path);
Xmlbeanfactory xmlbeanfactory = new Xmlbeanfactory (Resource);
Xmlbeanfactory.addbeanpostprocessor (New Mybeanpostprocessor ());
Xmlbeanfactory.addbeanpostprocessor (New Myinstantiationawarebeanpostprocessor ());
Car car = (car) xmlbeanfactory.getbean ("Car1");
Car car2 = (car) xmlbeanfactory.getbean ("Car1");
System.out.println (Car.tostring ());
Xmlbeanfactory.destroybean ("Car1");
}
Bean life cycle