How to call a method after bean initialization is complete?

SpringInitializationStartupApplicationcontext

Spring Problem Overview


I have a use case where I need to call a (non-static) method in the bean only-once at the ApplicationContext load up. Is it ok, if I use MethodInvokingFactoryBean for this? Or we have a some better solution?

As a side note, I use ConfigContextLoaderListener to load the Application Context in web application. And want, that if bean 'A' is instantiated just call methodA() once.

How can this be done nicely?

Spring Solutions


Solution 1 - Spring

To expand on the @PostConstruct suggestion in other answers, this really is the best solution, in my opinion.

  • It keeps your code decoupled from the Spring API (@PostConstruct is in javax.*)
  • It explicitly annotates your init method as something that needs to be called to initialize the bean
  • You don't need to remember to add the init-method attribute to your spring bean definition, spring will automatically call the method (assuming you register the annotation-config option somewhere else in the context, anyway).

Solution 2 - Spring

You can use something like:

<beans>
    <bean id="myBean" class="..." init-method="init"/>
</beans>

This will call the "init" method when the bean is instantiated.

Solution 3 - Spring

There are three different approaches to consider, as described in the reference

Use init-method attribute

Pros:
  • Does not require bean to implement an interface.
Cons:
  • No immediate indication in source code that this method is required after construction to ensure the bean is correctly configured.

Implement InitializingBean

Pros:
  • No need to specify init-method, or turn on component scanning / annotation processing.
  • Appropriate for beans supplied with a library, where we don't want the application using this library to concern itself with bean lifecycle.
Cons:
  • More invasive than the init-method approach.

Use JSR-250 @PostConstruct lifecyle annotation

Pros:
  • Useful when using component scanning to autodetect beans.
  • Makes it clearer that a specific method is to be used for initialisation. Intent is closer to the code.
Cons:
  • Initialisation no longer centrally specified in configuration.
  • You must remember to turn on annotation processing (which can sometimes be forgotten)

Solution 4 - Spring

Have you tried implementing InitializingBean? It sounds like exactly what you're after.

The downside is that your bean becomes Spring-aware, but in most applications that's not so bad.

Solution 5 - Spring

You could deploy a custom BeanPostProcessor in your application context to do it. Or if you don't mind implementing a Spring interface in your bean, you could use the InitializingBean interface or the "init-method" directive (same link).

Solution 6 - Spring

To further clear any confusion about the two approach i.e use of

  1. @PostConstruct and
  2. init-method="init"

From personal experience, I realized that using (1) only works in a servlet container, while (2) works in any environment, even in desktop applications. So, if you would be using Spring in a standalone application, you would have to use (2) to carry out that "call this method after initialization.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpeakitView Question on Stackoverflow
Solution 1 - SpringskaffmanView Answer on Stackoverflow
Solution 2 - SpringMercer TraiesteView Answer on Stackoverflow
Solution 3 - SpringtoolkitView Answer on Stackoverflow
Solution 4 - SpringJon SkeetView Answer on Stackoverflow
Solution 5 - SpringRob HView Answer on Stackoverflow
Solution 6 - SpringAyorindeView Answer on Stackoverflow