Pages

Friday, January 6, 2012

Spring Framework - call a method after bean initialization is complete

If you need to call a method after your bean is initiated in spring context then you can choose from few options.

1. Implement InitializingBean interface in your class.

class MyBean implements InitializingBean {

    public void afterPropertiesSet() throws Exception {
       System.out.println("my bean is ready");
    }

}
2. Use annotation @PostConstruct in your class. Don't forget to use also <annotation-config></annotation-config> in your application context.

class MyBean {

  @PostConstruct
  public void initMethod() throws Exception {
     System.out.println("my bean is ready");
  }

}
3. Use init method xml attribute.

class MyBean {

  public void initMethod() throws Exception {
     System.out.println("my bean is ready");
  }

}

0 comments:

Post a Comment