springannotationscastle-windsor

is there something like @predestroy in the spring as in the castle windsor


Anything like @PreDestroy in the spring-framework?


Solution

  • If you define a bean that implements the DisposableBean interface then Spring will call the

    void destroy() throws Exception;
    

    method before destrying the bean.

    That's one way, the other is when your bean doesn't have to implement the given interface. In one of yours ConfigurationSupport classes your bean has to be defined as as pulic method with the @Bean annotation.

       @Bean (destroyMethod="yourDestroyMethod")
       public YourBean yourBean() {
          YourBean yourBean = new YourBean();
    
          return yourBean;
       }
    

    The method "yourDestroyMethod" has to be defined in YourBean.class and then Spring will call it before destroying the bean.

    For more info see the Spring documentation: Destruction callbacks

    UPDATE

    The third way... I would even say the better way would be to specifiy "init-method" and "destroy-method" of your bean... like this: mkyong.com/spring/spring-init-method-and-destroy-method-example

    This solves the problem ot third-party dependency beans, and liberates the the code unnecessary Spring interfaces..