Rumah  >  Artikel  >  Java  >  Contoh analisis pengurusan kitaran hayat Java Spring Bean

Contoh analisis pengurusan kitaran hayat Java Spring Bean

王林
王林ke hadapan
2023-04-18 09:13:441458semak imbas

    Pengurusan kitaran hayat Spring Bean

    1. 🎜>2. Nyatakan kaedah permulaan dan pemusnahan Bean melalui parameter @Bean (initMethod, destroyMethod)

    1 Struktur projek Contoh analisis pengurusan kitaran hayat Java Spring Bean

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

    2

    3. Kelas konfigurasi pendaftaran kacang (contoh tunggal) Contoh analisis pengurusan kitaran hayat Java Spring Bean

    4. Kelas ujian
    public class Person {
        public Person(){
            System.out.println("Person 创建了...");
        }
        public void init(){
            System.out.println("Person 初始化了...");
        }
        public void destroy(){
            System.out.println("Person 被销毁了...");
        }
    }
    5 >
    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();
        }
    }
    6. Kelas konfigurasi pendaftaran kacang (berbilang kejadian)
    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();
        }
    }
    7. Keputusan ujian

    Contoh analisis pengurusan kitaran hayat Java Spring Bean

    3. Bean Melaksanakan antara muka InitializingBean, DisposableBean
    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();
        }
    }
    1 Orang

    2 kelas konfigurasi pendaftaran BeanContoh analisis pengurusan kitaran hayat Java Spring Bean

    3

    4 Melalui anotasi @PostConstruct dan @PreDestroy
    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 被销毁了...");
        }
    }
    1 Orang
    import com.dashu.bean.Person;
    import org.springframework.context.annotation.*;
    @Configuration
    public class BeanConfig {
        @Bean
        public Person person(){
            return new Person();
        }
    }
    hasil ujian

    Contoh analisis pengurusan kitaran hayat Java Spring Bean

    5 Gunakan antara muka BeanPostProcessor untuk melaksanakan kelas (post-processor)
    @PostConstruct:标注在Bean的初始化方法上
    @PreDestroy:标注在Bean的销毁方法上
    1 Struktur projek
    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. OrangContoh analisis pengurusan kitaran hayat Java Spring Bean

    3. Kelas konfigurasi pendaftaran kacang

    4. Kelas pelaksanaan BeanPostProcessor

    Contoh analisis pengurusan kitaran hayat Java Spring Bean

    5 >
    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 被销毁了...");
        }
    }

    Atas ialah kandungan terperinci Contoh analisis pengurusan kitaran hayat Java Spring Bean. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

    Kenyataan:
    Artikel ini dikembalikan pada:yisu.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam