Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Something in Spring like 'init-method' but called after dependencies are injected?

Tags:

spring

This is crazy... been using Spring for a while but can't find something like the "init-method" that gets invoked AFTER all the dependencies have been injected.

I saw the BeanPostProcessor thingie but I am looking for something lightweight and non-intrusive that doesn't couple my beans to Spring. Like the init-method!

like image 205
Kevin Pauli Avatar asked Aug 12 '10 14:08

Kevin Pauli


1 Answers

With Spring 2.5 and above, if an object requires invocation of a callback method upon initialization, that method can be annotated with the @PostConstruct annotation.

For example:

public class MyClass{

   @PostConstruct
   public void myMethod() {
     ...
   }
   ...
} 

This is less intrusive than the BeanPostProcessor approach.

like image 80
Jose Diaz Avatar answered Oct 25 '22 07:10

Jose Diaz