Initialization and destruction of Spring Bean

Posted by alireza on Tue, 07 Jan 2020 00:58:03 +0100

A little bit of eye

In the development process, it is often encountered to do some necessary operations before or after the Bean is used. Spring provides support for the operation of the Bean's life cycle.

1. Java configuration mode: use initMethod and destroyMethod of @ Bean.

2. Annotation method: use JSR-250's @ PostConstruct and @ PreDestroy.

Two actual combat

1. Add JSR250 support

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>jsr250-api</artifactId>
    <version>1.0</version>
</dependency>

2. Use @ Bean

package com.wisely.highlight_spring4.ch2.prepost;

public class BeanWayService {
      public void init(){
            System.out.println("@Bean-init-method");
        }
        public BeanWayService() {
            super();
            System.out.println("Initialize constructor-BeanWayService");
        }
        public void destroy(){
            System.out.println("@Bean-destory-method");
        }
}

3. Use a Bean in the form of JSR250

package com.wisely.highlight_spring4.ch2.prepost;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class JSR250WayService {
    @PostConstruct //Execute after constructor execution
    public void init(){
        System.out.println("jsr250-init-method");
    }
    public JSR250WayService() {
        super();
        System.out.println("Initialize constructor-JSR250WayService");
    }
    @PreDestroy //Execute before Bean is destroyed
    public void destroy(){
        System.out.println("jsr250-destory-method");
    }

}

4. Write configuration class

package com.wisely.highlight_spring4.ch2.prepost;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch2.prepost")
public class PrePostConfig {
    //initMethod and destroyMethod specify the
    //init and destroy methods are executed after the constructor and before the Bean is destroyed
    @Bean(initMethod="init",destroyMethod="destroy")
    BeanWayService beanWayService(){
        return new BeanWayService();
    }
    
    @Bean
    JSR250WayService jsr250WayService(){
        return new JSR250WayService();
    }
}

5. Write main class

package com.wisely.highlight_spring4.ch2.prepost;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(PrePostConfig.class);
        
        BeanWayService beanWayService = context.getBean(BeanWayService.class);
        JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class);
        
        context.close();
    }

}

Three test

August 06, 2018 9:53:47 PM org.springframework.context.annotation.annotationconfidapplication context preparerefresh

Information: refreshing org.springframework.context.annotation.annotationconfidapplicationcontext @ 1f17ae12: startup date [mon Aug 06 21:53:47 CST 2018]; root of context hierarchy

Initialize constructor - BeanWayService

@Bean-init-method

Initialization constructor - JSR250WayService

jsr250-init-method

August 06, 2018 9:53:47 PM org.springframework.context.annotation.annotationconfidapplicationcontext doclose

Information: closing org.springframework.context.annotation.annotationconfidapplicationcontext @ 1f17ae12: startup date [mon Aug 06 21:53:47 CST 2018]; root of context hierarchy

jsr250-destory-method

@Bean-destory-method

Topics: Spring Java