Executing code on the creation or destruction of a bean in Spring

There are 3 ways to execute code to create a bean in Spring:

  • with the annotation @PostConstruct
  • implementing the interface InitializingBean
  • configuring the property init-method

The reason why you run code when you create a bean is often to check the dependencies, and assign a default value, or automated actions such as starting a scheduled task.
The constructor of the bean may not be suitable for this purpose because the bean is not fully initialized until the constructor has not completed.

In a similar way there are 3 ways to execute code in the destruction of a bean in Spring:

  • with the annotation @PreDestroy
  • implementing the interface DisposableBean
  • configuring the property destroy-method

Usually these methods are used to free resources such as open files but notice that they are only called for singleton bean (single instance of the bean in the application); in the case of prototype bean only the destroy method of the DisposableBean interface is called.
In any case you need an explicit call to a destroy method (of AbstractApplicationContext or the bean), it is not enough that the application simply ends.
An alternative is to use the method AbstractApplicationContext.registerShutdownHook() with which you create a thread just before closing the application that calls the destroy method of AbstractApplicationContext.
The following table shows all possibilities:

singleton bean prototype bean
AbstractApplicationContext.destroy() @PreDestroy
no methods
bean.destroy() DisposableBean.destroy() DisposableBean.destroy()
AbstractApplicationContext.registerShutdownHook() @PreDestroy
no methods

Of course, not all methods must be implemented at the same time but you can choose which to use.

pro con
methods configured in file .xml decoupled from Spring manual configuration of every bean
methods inherited from interfaces configuration of only one class, portability coupled to Spring
methods with annotation portability required support for JSR-250

To test this, create a new java project:

  1. create the file conf/app-context.xml

    the directory “conf” must be in the classpath;
    you need the row <context:annotation-config/> if you use the annotations
  2. create the file conf/log4j.xml
  3. create the file eu/lucazanini/mybean/Main.java

    the class GenericXmlApplicationContext is a subclass of AbstractApplicationContext
  4. create the file eu/lucazanini/mybean/SimpleBean.java

The output is the following:

See that the order in which these methods are executed is fixed:

  1. method from annotation (@PostConstruct e @PreDestroy)
  2. method from interfaces (InitializingBean e DisposableBean)
  3. method from file .xml (init-method e destroy-method)

if you comment and uncomment some parts of the file Main.java or replacing “singleton” with “prototype” as scope of the bean in app-context.xml, you can get the other output shown in this post.

One Reply to “Executing code on the creation or destruction of a bean in Spring”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.