Wednesday, August 4, 2010

Life Cycle of a Bean in Spring


Life Cycle of a Bean in Spring

A Spring Bean is managed by IoC Container / Spring Container, whenever a bean is declared in the Spring configuration file, container will find it and do the following steps to make available for the application.

1) Firstly, spring creates an instance of the Bean using Java Reflection API.
2) If there are any properties associated with bean then spring injects those properties. If there are any bean as a peoperty then spring finds that bean and inject it.
3) If the Bean class implements the BeanNameAware interface, then the spring will call setBeanName() method by passing the name of the Bean.
4) If the Bean class implements the BeanFactoryAware interface, then the spring will call setBeanFactory() method by passing an instance of BeanFactory object.
5) If there are any BeanPostProcessors associated with the BeanFactory that loads the Bean, then the spring will call postProcessBeforeInitialization() method before the properties for the Bean are injected.
6) If the Bean class implements the InitializingBean interface, then the spring will call afterPropertiesSet() method once all the Bean properties defined in the Configuration file are injected.
7) If there is any custom init-method declared in the configuration file, that method will be called.
8) If there are any BeanPostProcessors associated with the BeanFactory that loads the Bean, then the spring will call postProcessAfterInitialization() method.
9) Now bean is ready to use.
10) If the Bean class implements the DisposableBean interface, then the spring calls destroy() method when the Application no longer needs the bean reference.
11) If there is any custom destroy-method declared in the configuration file, that method will be called.

28 comments:

  1. Nice post, with diagram clearly showing steps. Easy to remember as well.

    ReplyDelete
  2. In case of ApplicationContext, its slightly changes the behavior :
    After (3) there will be one more,
    If the Bean class implements the ApplicationContextAware interface, then the spring will call setApplicationContext() method.

    ReplyDelete
  3. Hi there, at the second point you say " If there are any bean as a peoperty then spring finds that bean and inject it." but at fifth point "before the properties for the Bean are injected" you negate second point?

    ReplyDelete
  4. Usefull post. Great material.
    Keep it up
    Thanks for sharing.

    http://wirelesscellularnetwork.blogspot.com/2017/06/wcdma-intra-frequency-handover.html

    ReplyDelete
  5. Thank You for this useful blog!

    1croreprojects is the team looks forward to welcoming you to our project development centre and help you to satisfied is assist in mastering new technology developments and presenting necessary assist to enforce their academic tasks on slicing area technology in Hybrid Cloud Approach for Secure Authorized Deduplication.

    ReplyDelete
  6. Thanks for sharing your knowledge with us .This will absolutely going to help me in my future .

    JAVA J2EE Training in Chennai

    J2EE Training in Chennai

    ReplyDelete
  7. Thank you for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. sap abap training online

    ReplyDelete
  8. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
    Digital Marketing online training

    full stack developer training in pune

    full stack developer training in annanagar


    ReplyDelete
  9. This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge. Excellent post.


    This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge. Excellent post.

    java training in bangalore

    aws training in bangalore

    ReplyDelete
  10. This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge. Excellent post.

    java training in bangalore

    aws training in bangalore

    ReplyDelete
  11. You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us

    java training in chennai

    ReplyDelete
  12. I am very happy when read this blog post because blog post written in good manner and write on good topic. Thanks for sharing valuable information.

    white label website builder

    ReplyDelete
  13. Sap fico training institute in Noida

    Sap fico training institute in Noida - Webtrackker Technology is IT Company which is providing the web designing, development, mobile application, and sap installation, digital marketing service in Noida, India and out of India. Webtrackker is also providing the sap fico training in Noida with working trainers.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.

    +91 - 8802820025
    0120-433-0760
    0120-4204716
    EMAIL: info@webtrackker.com
    Website: www.webtrackker.com

    ReplyDelete
  14. I ReGreat For Your Information The Information U have Shared Is Fabulous And Interesting So Please keep Updating Us The Information Shared Is Very Valuable Time Just Went On Reading The Article Python Online Course AWS Online Course Data Science Online Course Hadoop Online Course

    ReplyDelete
  15. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information.
    Python Training in Noida

    ReplyDelete
  16. This is very nice information, Thank you so much for sharing your knowledge. Keep sharing!
    Core PHP Training Institute in Noida
    Python Training Classes in Noida

    ReplyDelete
  17. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man, Keep it up.
    Oracle Training in Medavakkam / Best Oracle Training in Medavakkam
    Oracle Training Course in Chennai / Best Oracle Training Institute in Chennai

    ReplyDelete
  18. Thanks for sharing your knowledge with us .This will absolutely going to help me in my future .
    AI and Web Search Monitor user’s task Seek needed information Learn which information is most useful
    AI Training in Bangalore

    AI Course in Bangalore

    ReplyDelete