Home  >  Article  >  Java  >  Example analysis of Java Spring Bean life cycle management

Example analysis of Java Spring Bean life cycle management

王林
王林forward
2023-04-18 09:13:441441browse

    Life cycle management of Spring Bean

    1. Life cycle of Spring Bean

    Example analysis of Java Spring Bean life cycle management

    通过以下方式来指定Bean的初始化和销毁方法,
    当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法
    当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法

    2. Specify the initialization and destruction methods of the Bean through @Bean parameters (initMethod, destroyMethod)

    1. Project structure

    Example analysis of Java Spring Bean life cycle management

    2. Person
    public class Person {
        public Person(){
            System.out.println("Person 创建了...");
        }
        public void init(){
            System.out.println("Person 初始化了...");
        }
        public void destroy(){
            System.out.println("Person 被销毁了...");
        }
    }
    3. Bean registration configuration class (single instance)
    import com.dashu.bean.Person;
    import org.springframework.context.annotation.*;
    @Configuration
    public class BeanConfig {
        @Bean(initMethod = "init",destroyMethod = "destroy")
        public Person person(){
            return new Person();
        }
    }
    4. Test class
    import com.dashu.bean.Person;
    import com.dashu.config.BeanConfig;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    public class Main {
        public static void main(String[] args) {
            //加载配置类获取容器
            AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
            //获取Bean
            Person bean = annotationConfigApplicationContext.getBean(Person.class);
            //关闭容器
            annotationConfigApplicationContext.close();
        }
    }
    5. Test results

    Example analysis of Java Spring Bean life cycle management

    6. Bean registration configuration class (multiple instances)
    import com.dashu.bean.Person;
    import org.springframework.context.annotation.*;
    @Configuration
    public class BeanConfig {
        @Scope("prototype")
        @Bean(initMethod = "init",destroyMethod = "destroy")
        public Person person(){
            return new Person();
        }
    }
    7. Test results

    Example analysis of Java Spring Bean life cycle management

    ##3. Bean implementation interface InitializingBean, DisposableBean

    1. Person
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    public class Person implements InitializingBean, DisposableBean {
        public Person(){
            System.out.println("Person 创建了...");
        }
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("Person 初始化了...");
        }
        @Override
        public void destroy() throws Exception {
            System.out.println("Person 被销毁了...");
        }
    }
    2. Bean registration configuration class
    import com.dashu.bean.Person;
    import org.springframework.context.annotation.*;
    @Configuration
    public class BeanConfig {
        @Bean
        public Person person(){
            return new Person();
        }
    }
    3. Test results

    Example analysis of Java Spring Bean life cycle management##4. Through the annotation @PostConstruct and @PreDestroy

    @PostConstruct:标注在Bean的初始化方法上
    @PreDestroy:标注在Bean的销毁方法上

    1, Person

    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    public class Person {
        public Person(){
            System.out.println("Person 创建了...");
        }
        @PostConstruct
        public void init(){
            System.out.println("Person 初始化了...");
        }
        @PreDestroy
        public void destroy(){
            System.out.println("Person 被销毁了...");
        }
    }
    2, Test results

    Example analysis of Java Spring Bean life cycle management5. Use the interface BeanPostProcessor implementation class (post-processor )

    1. Project structure

    Example analysis of Java Spring Bean life cycle management2. Person

    import org.springframework.stereotype.Component;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    @Component
    public class Person {
        public Person(){
            System.out.println("Person 创建了...");
        }
        @PostConstruct
        public void init(){
            System.out.println("Person 初始化了...");
        }
        @PreDestroy
        public void destroy(){
            System.out.println("Person 被销毁了...");
        }
    }
    3. Bean registration configuration class
    import org.springframework.context.annotation.*;
    @Configuration
    @ComponentScan({"com.dashu"})
    public class BeanConfig {
    }
    4 , BeanPostProcessor implementation class (post-processor)
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
    import org.springframework.lang.Nullable;
    import org.springframework.stereotype.Component;
    /**
     * 后置处理器:初始化前后进行处理工作
     */
    @Component
    public class MyBeanPostProcessor implements BeanPostProcessor {
        /**
         * 初始化之前工作
         * @param bean
         * @param beanName
         * @return
         * @throws BeansException
         */
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("初始化之前..."+beanName+"=["+bean+"]");
            return bean;
        }
        /**
         * 初始化之后工作
         * @param bean
         * @param beanName
         * @return
         * @throws BeansException
         */
        @Override
        public  Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("初始化之后..."+beanName+"=["+bean+"]");
            return bean;
        }
    }
    5, test results

    The above is the detailed content of Example analysis of Java Spring Bean life cycle management. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete