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.
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.
Nice post, with diagram clearly showing steps. Easy to remember as well.
ReplyDeleteIn case of ApplicationContext, its slightly changes the behavior :
ReplyDeleteAfter (3) there will be one more,
If the Bean class implements the ApplicationContextAware interface, then the spring will call setApplicationContext() method.
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?
ReplyDeleteUsefull post. Great material.
ReplyDeleteKeep it up
Thanks for sharing.
http://wirelesscellularnetwork.blogspot.com/2017/06/wcdma-intra-frequency-handover.html
ReplyDeletethank you seo training in chennai
hibernate training in chennai
spring training in chennai
Thank you !! Very usefull !!
ReplyDeleteieee java projects in chennai
final year arduino project centers chennai
Web Design training in chennai
digital marketing company in chennai
Thank you !! Very usefull !!
ReplyDeletedot net training in chennai
matlab training in chennai
embedded system training in chennai
Thanks for sharing this blog!
ReplyDeletems projects chennai
asp mini projects in chennai
Thank You for this useful blog!
ReplyDelete1croreprojects 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.
Thanks for sharing your knowledge with us .This will absolutely going to help me in my future .
ReplyDeleteJAVA J2EE Training in Chennai
J2EE Training in Chennai
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
ReplyDeleteIt 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.
ReplyDeleteDigital Marketing online training
full stack developer training in pune
full stack developer training in annanagar
This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge. Excellent post.
ReplyDeleteThis 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
This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge. Excellent post.
ReplyDeletejava training in bangalore
aws training in bangalore
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
ReplyDeletejava training in chennai
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.
ReplyDeletewhite label website builder
Thank you and very useful
ReplyDeletejava training in Bangalore
spring training in Bangalore
java training institute in Bangalore
spring and hibernate training in Bangalore
Nice Post..
ReplyDeletejava training in BTM
spring training in BTM
java training institute in btm
spring and hibernate training in btm
Thanks for sharing this useful information
ReplyDeletejava training in Marathahalli
spring training in Marathahalli
java training institute in Marathahalli
spring and hibernate training in Marathahalli
Nice Blog
ReplyDeleteangularjs training in Bangalore
angularjs training institutes in Bangalore
best angularjs training in Bangalore
Sap fico training institute in Noida
ReplyDeleteSap 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
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
ReplyDeleteNice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information.
ReplyDeletePython Training in Noida
This is very nice information, Thank you so much for sharing your knowledge. Keep sharing!
ReplyDeleteCore PHP Training Institute in Noida
Python Training Classes in Noida
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.
ReplyDeleteOracle Training in Medavakkam / Best Oracle Training in Medavakkam
Oracle Training Course in Chennai / Best Oracle Training Institute in Chennai
Thank you for sharing your thoughts and knowledge on this topic.
ReplyDeleteFull Stack Developer Online Training
full stack training in hyderabad
Full Stack Training in Ameerpet
Thank you for sharing such great information very useful to us
ReplyDeleteIt is very useful.Thanks for sharing.
oracle training in chennai
oracle training institute in chennai
oracle training in bangalore
oracle training in hyderabad
oracle training
hadoop training in chennai
hadoop training in bangalore
Thanks for sharing your knowledge with us .This will absolutely going to help me in my future .
ReplyDeleteAI and Web Search Monitor user’s task Seek needed information Learn which information is most useful
AI Training in Bangalore
AI Course in Bangalore