Bean initialization process in spring

Source: Internet
Author: User

In traditional Java applications, bean lifecycle is very simple. Java keyword new is used to instantiate Bean (maybe non-serialized ). This is enough. On the contrary, bean lifecycle is more detailed in spring containers. Understanding the lifecycle of spring bean is very important, because you may need to use the opportunity provided by spring to customize the bean creation process.

1. The Container looks for bean definition information and instantiates it.
2. Use dependency injection. Spring configures all bean attributes according to bean definition information.
3. If the bean implements the beannameaware interface, the factory calls the setbeanname () method of bean to pass the bean ID.
4. If bean implements the beanfactoryaware interface, the factory calls the setbeanfactory () method to pass it to the factory itself.
5. If beanpostprocessor is associated with bean, their postprocessbeforeinitialation () method will be called.
6. If the bean specifies the init-method, it will be called.
7. Finally, if beanpsotprocessor is associated with bean, their postprocessafterinitialization () method will be called.
At this time, the bean can be used by the application system and will be kept in the Bean factory to know that it is no longer needed. There are two ways to delete it from Bean factory.
1. If bean implements the disposablebean interface, the destory () method is called.
2. If a custom destruction method is specified, call this method.
The life cycle of bean in spring application context is only different from that in Bean factory. The only difference is that if bean implements the applicationcontextawre interface, the setapplicationcontext () method is called.

1. The Container looks for bean definition information and instantiates it.
2. Use dependency injection. Spring configures all bean attributes according to bean definition information.
3. If the bean implements the beannameaware interface, the factory calls the setbeanname () method of bean to pass the bean ID.
4. If bean implements the beanfactoryaware interface, the factory calls the setbeanfactory () method to pass it to the factory itself.
5. If beanpostprocessor is associated with bean, their postprocessbeforeinitialation () method will be called.
6. If the bean specifies the init-method, it will be called.
7. Finally, if beanpsotprocessor is associated with bean, their postprocessafterinitialization () method will be called.

At this time, the bean can be used by the application system and will be kept in the Bean factory to know that it is no longer needed. There are two ways to delete it from Bean factory.

1. If bean implements the disposablebean interface, the destory () method is called.
2. If a custom destruction method is specified, call this method.

The life cycle of bean in spring application context is only different from that in Bean factory. The only difference is that if bean implements the applicationcontextawre interface, the setapplicationcontext () method is called.

 

Example: (here is only an example of Bean Factory)
1. helloworld. Java

Java code
  1. PackageCom. Spring. lifecycle;
  2. ImportOrg. springframework. Beans. beansexception;
  3. ImportOrg. springframework. Beans. Factory. beanfactory;
  4. ImportOrg. springframework. Beans. Factory. beanfactoryaware;
  5. ImportOrg. springframework. Beans. Factory. beannameaware;
  6. ImportOrg. springframework. Beans. Factory. disposablebean;
  7. ImportOrg. springframework. Beans. Factory. initializingbean;
  8. ImportOrg. springframework. Beans. Factory. config. beanpostprocessor;
  9. Public ClassHelloworldImplementsBeannameaware, beanfactoryaware, beanpostprocessor, initializingbean, disposablebean {
  10. PrivateString hello;
  11. Public VoidSetbeanname (string arg0 ){
  12. System. Out. println ("Call beannameaware's setbeanname ()...");
  13. }
  14. PublicString gethello (){
  15. ReturnHello;
  16. }
  17. Public VoidSethello (string Hello ){
  18. This. Hello = Hello;
  19. System. Out. println ("Call sethello ()...");
  20. }
  21. Public VoidCustominit (){
  22. System. Out. println ("Call custominit ()...");
  23. }
  24. Public VoidCustomdestroy (){
  25. System. Out. println ("Call customdestroy ()...");
  26. }
  27. PublicObject postprocessafterinitialization (Object arg0, string arg1)
  28. ThrowsBeansexception {
  29. System. Out. println ("Call beanpostprocessor's postprocessafterinitialization ()...");
  30. Return Null;
  31. }
  32. PublicObject postprocessbeforeinitialization (Object arg0, string arg1)
  33. ThrowsBeansexception {
  34. System. Out. println ("Call beanpostprocessor's postprocessbeforeinitialization ()...");
  35. Return Null;
  36. }
  37. Public VoidDestroy ()ThrowsException {
  38. System. Out. println ("Call destory ()..." of disposablebean ()...");
  39. }
  40. Public VoidAfterpropertiesset ()ThrowsException {
  41. System. Out. println ("Call the afterpropertiesset ()..." of initializingbean ()...");
  42. }
  43. Public VoidSetbeanfactory (beanfactory arg0)ThrowsBeansexception {
  44. System. Out. println ("Call beanfactoryaware's setbeanfactory ()...");
  45. }
  46. }

2. Test Procedures

Java code
  1. PackageCom. Spring. springtest;
  2. ImportJUnit. Framework. testcase;
  3. ImportOrg. springframework. Beans. Factory. beanfactory;
  4. ImportOrg. springframework. Context. Support. classpathxmlapplicationcontext;
  5. ImportCom. Spring. lifecycle. helloworld;
  6. Public ClassTestlifecycleExtendsTestcase {
  7. PrivateBeanfactory BF;
  8. Protected VoidSetup ()ThrowsException {
  9. BF =NewClasspathxmlapplicationcontext ("applicationcontext. xml ");
  10. }
  11. Public VoidTestlifecycle ()ThrowsException {
  12. Helloworld Hello = (helloworld) BF. getbean ("helloworld ");
  13. Assertequals ("Hello world! ", Hello. gethello ());
  14. Hello. Destroy ();
  15. }
  16. }

3. applicationcontext. xml

XML Code
  1. <? XMLVersion = "1.0" encoding = "UTF-8"?>
  2. <! Doctype beans public "-// spring // DTD bean // en" http://www.springframework.org/dtd/spring-beans.dtd">
  3. <Beans>
  4. <BeanId = "helloworld" class = "com. Spring. lifecycle. helloworld"
  5. Init-method = "custominit" Destroy-method = "customdestroy">
  6. <PropertyName = "hello" value = "Hello world! "> </Property>
  7. </Bean>
  8. </Beans>

4. Running result:

Reference call sethello ()...
Call beannameaware's setbeanname ()...
Call beanfactoryaware's setbeanfactory ()...
Call the afterpropertiesset () of initializingbean ()...
Call custominit ()...
Call destory () of disposablebean ()...

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.