Turn-spring bean processing-initializingbean&beanpostprocessor&beanfactorypostprocessor

Source: Internet
Author: User

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
    1. @Component ("Beana")
    2. public class Beana implements Initializingbean {
    3. /**
    4. * Callback function that is called after the Bean property setting is complete
    5. */
    6. public void Afterpropertiesset () throws Exception {
    7. SYSTEM.OUT.PRINTLN ("callback function, which is called after the Bean property setting is complete");
    8. }
    9. }

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
  1. @Component
  2. public class Mybeanpostprocessor implements Beanpostprocessor {
  3. /**
  4. * Any Bean object will call Beanpostprocessor after the initialization method callback.
  5. * Postprocessafterinitialization method. We can then do a layer of encapsulation in the method body in the face of the returned bean.
  6. * Before calling this method, the bean object we passed in is already populated with the property values. When we take beanpostprocessor as
  7. * When a bean is defined in ApplicationContext, ApplicationContext will automatically detect it and treat it as
  8. * One beanpostprocessor is called.
  9. */
  10. public object Postprocessafterinitialization (object bean, String beanname)
  11. Throws Beansexception {
  12. System.out.println (Bean + "After initialization, Beanname is" + beanname);
  13. return bean;
  14. }
  15. /**
  16. * Any Bean object will call Beanpostprocessor before the initialization method callback.
  17. * Postprocessbeforeinitialization method. Before calling the method, we pass in the
  18. * The Bean object is already populated with the property values.
  19. */
  20. public object Postprocessbeforeinitialization (object bean, String beanname)
  21. Throws Beansexception {
  22. System.out.println (Bean + "beforeinitialization, Beanname is" + beanname);
  23. return bean;
  24. }
  25. }

Beanfactorypostprocessor Interface Java code
  1. 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.
  2. @Component
  3. public class Mybeanfactorypostprocessor implements Beanfactorypostprocessor {
  4. /**
  5. * Beanfactorypostprocessor's Postprocessbeanfactory () method is initialized at the current beanfactory
  6. * 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
  7. * Beanfactory do some operations. When we define beanfactorypostprocessor as a bean in ApplicationContext,
  8. * ApplicationContext will automatically detect it and call it as a beanfactorypostprocessor.
  9. */
  10. public void Postprocessbeanfactory (
  11. Configurablelistablebeanfactory beanfactory) throws Beansexception {
  12. System.out.println ("Postprocessbeanfactory ...");
  13. }
  14. }

Turn-spring bean processing-initializingbean&beanpostprocessor&beanfactorypostprocessor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.