Transfer from http://elim.iteye.com/blog/2017466
Execution order:
Beanfactorypostprocessor.postprocessbeanfactory
UserBean ' s constructor
UserBean ' s Username property set
Beanpostprocessor.postprocessbeforeinitialization
Initializingbean.afterpropertiesset
Beanpostprocessor.postprocessafterinitialization
Spring Bean processing--callback function
Spring defines three interfaces that can be used to process the beanfactory of a spring bean or a generated bean, Initializingbean, Beanpostprocessor and Beanfactorypostprocessor. By implementing these three interfaces we can process the spring bean.
Initializingbean interface
A Afterpropertiesset () method is defined in Initializingbean. When beanfactory instantiates our bean and sets the corresponding property, if our Bean implements the Initializingbean interface, the corresponding Afterpropertiesset () method is called. Then we can make changes to the properties of the current bean in this method body and other operations.
Java code
- @Component ("Beana")
- public class Beana implements Initializingbean {
- /**
- * Callback function that is called after the Bean property setting is complete
- */
- public void Afterpropertiesset () throws Exception {
- SYSTEM.OUT.PRINTLN ("callback function, which is called after the Bean property setting is complete");
- }
- }
Beanpostprocessor interface
The Beanpostprocessor interface implementation class can do some processing of beans before and after bean initialization. ApplicationContext can automatically detect if a bean has implemented the Beanpostprocessor interface, and if it has already implemented it it will treat it as a beanpostprocessor. Then make the call when you need to call beanpostprocessor. There are two methods defined in Beanpostporcessor, Postprocessbeforeinitialization () and Postprocessafterinitialization ().
The L postprocessbeforeinitialization (Object Bean, String beanname) method is called before the initialization method of the bean is called. The method parameters represent the current Bean object and the corresponding bean name, respectively.
The L postprocessafterinitialization (Object Bean, String beanname) method is called after the initialization method of the bean is called.
The beanpostprocessor is for all the beans in the container. Once the container is defined with beanpostprocessor, each bean in the container will call the Beanpostprocessor corresponding method before and after initialization.
Java code
- @Component
- public class Mybeanpostprocessor implements Beanpostprocessor {
- /**
- * Any Bean object will call Beanpostprocessor after the initialization method callback.
- * Postprocessafterinitialization method. We can then do a layer of encapsulation in the method body in the face of the returned bean.
- * Before calling this method, the bean object we passed in is already populated with the property values. When we take beanpostprocessor as
- * When a bean is defined in ApplicationContext, ApplicationContext will automatically detect it and treat it as
- * One beanpostprocessor is called.
- */
- public object Postprocessafterinitialization (object bean, String beanname)
- Throws Beansexception {
- System.out.println (Bean + "After initialization, Beanname is" + beanname);
- return bean;
- }
- /**
- * Any Bean object will call Beanpostprocessor before the initialization method callback.
- * Postprocessbeforeinitialization method. Before calling the method, we pass in the
- * The Bean object is already populated with the property values.
- */
- public object Postprocessbeforeinitialization (object bean, String beanname)
- Throws Beansexception {
- System.out.println (Bean + "beforeinitialization, Beanname is" + beanname);
- return bean;
- }
- }
Beanfactorypostprocessor Interface Java code
- The Beanfactorypostprocessor interface implementation class can do some processing of beanfactory before the bean is instantiated after the current beanfactory is initialized . Beanfactorypostprocessor is for the bean container, and when it is called, beanfactory only loads the bean's definition and does not instantiate them. So we can instantiate the effect of the bean after it has been processed by Beanfactory. Like Beanpostprocessor, ApplicationContext can also automatically detect and invoke Beanfactorypostprocessor in the container.
- @Component
- public class Mybeanfactorypostprocessor implements Beanfactorypostprocessor {
- /**
- * Beanfactorypostprocessor's Postprocessbeanfactory () method is initialized at the current beanfactory
- * Later, and all the bean definitions have been loaded, but no corresponding instances have been invoked. So we can do that in the body of the method
- * Beanfactory do some operations. When we define beanfactorypostprocessor as a bean in ApplicationContext,
- * ApplicationContext will automatically detect it and call it as a beanfactorypostprocessor.
- */
- public void Postprocessbeanfactory (
- Configurablelistablebeanfactory beanfactory) throws Beansexception {
- System.out.println ("Postprocessbeanfactory ...");
- }
- }
Turn-spring bean processing-initializingbean&beanpostprocessor&beanfactorypostprocessor