The spring container initializes the bean and takes action before destroying it

Source: Internet
Author: User

First: Actions before initializing and destroying beans by annotating @postconstruct and @PreDestroy methods

[Java]View Plain Copy
  1. Import javax.annotation.PostConstruct;
  2. Import Javax.annotation.PreDestroy;
  3. Public class datainitializer{
  4. @PostConstruct
  5. public void Initmethod () throws Exception {
  6. System.out.println ("Initmethod is executed");
  7. }
  8. @PreDestroy
  9. public void Destroymethod () throws Exception {
  10. System.out.println ("Destroymethod is executed");
  11. }
  12. }

The second is: by defining the Init-method and Destory-method methods in XML

[Java]View Plain Copy
  1. Public class datainitializer{
  2. public void Initmethod () throws Exception {
  3. System.out.println ("Initmethod is executed");
  4. }
  5. public void Destroymethod () throws Exception {
  6. System.out.println ("Destroymethod is executed");
  7. }
  8. }

[HTML]View Plain Copy
    1. <Bean id="Datainitializer" class="Com.somnus.demo.DataInitializer" init-method=" Initmethod " destory-method="destroymethod "/>

The third is the implementation of the Initializingbean and Disposablebean interfaces via beans

[Java]View Plain Copy
  1. Import Org.springframework.beans.factory.DisposableBean;
  2. Public class Datainitializer implements initializingbean,disposablebean{
  3. @Override
  4. public void Afterpropertiesset () throws Exception {
  5. System.out.println ("Afterpropertiesset is executed");
  6. }
  7. @Override
  8. public Void Destroy () throws Exception {
  9. System.out.println ("destroy is executed");
  10. }
  11. }

The first and the second are the same form, but one XML configuration, the other is the annotation form, there is a big difference is the third kind,

If a method is executed when the same bean is initialized in two ways, it is first shown in the order of execution.

Execute Afterpropertiesset () First,

Post-execution Initmethod ()

Here we look at the source code

How does this approach be implemented in spring?

By looking at the source Class (Abstractautowirecapablebeanfactory) of spring's load bean, you can see the secret

Abstractautowirecapablebeanfactory class in the Invokeinitmethods explained very clearly, the source code is as follows:

[Java]View Plain Copy
  1. protected void Invokeinitmethods (String beanname, final Object Bean, rootbeandefinition mbd)
  2. throws Throwable {
  3. ///To determine if the bean implements the Initializingbean interface, and if the Initializingbean interface is implemented, only the Afterpropertiesset method of invoking the bean is dropped
  4. Boolean Isinitializingbean = (bean instanceof Initializingbean);
  5. if (Isinitializingbean && (mbd = = Null | |!mbd.isexternallymanagedinitmethod ("Afterpropertiesset" ))) {  
  6. if (logger.isdebugenabled ()) {
  7. Logger.debug ("invoking Afterpropertiesset () on beans with name '" + beanname + "'");
  8. }
  9. if (system.getsecuritymanager () = null) {
  10. try {
  11. Accesscontroller.doprivileged (new privilegedexceptionaction<object> () {
  12. Public Object Run () throws Exception {
  13. //Call Afterpropertiesset directly
  14. ((Initializingbean) bean). Afterpropertiesset ();
  15. return null;
  16. }
  17. },getaccesscontrolcontext ());
  18. } catch (Privilegedactionexception PAE) {
  19. throw pae.getexception ();
  20. }
  21. }
  22. else {
  23. //Call Afterpropertiesset directly
  24. ((Initializingbean) bean). Afterpropertiesset ();
  25. }
  26. }
  27. if (mbd! = null) {
  28. String initmethodname = Mbd.getinitmethodname ();
  29. //Determines whether the Init-method method is specified, and if the Init-method method is specified, then the established Init-method is called
  30. if (initmethodname ! = NULL &&! ( Isinitializingbean && "Afterpropertiesset". Equals (initmethodname) &&
  31. !mbd.isexternallymanagedinitmethod (Initmethodname)) {
  32. ///To see the source of the method further, it can be found that the method specified in the Init-method method is implemented by reflection
  33. Invokecustominitmethod (Beanname, Bean, mbd);
  34. }
  35. }


Summarize:

1:spring provides two ways for the bean to initialize the bean, implement the Initializingbean interface, implement the Afterpropertiesset method, or in the configuration file

Init-method , two different ways can be used simultaneously


2: Implementing the Initializingbean interface is a direct call to the Afterpropertiesset method, which is more efficient than the method specified by the reflection call Init-method. But

Init-method Way to eliminate the reliance on spring


3: If an error occurs when calling the Afterpropertiesset method, the method specified by Init-method is not called.

The spring container initializes the bean and takes action before destroying it

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.