" />

最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Spring Bean的生命周期管理詳解

 更新時間:2021年12月23日 11:22:50   作者:大樹下躲雨  
這篇文章主要為大家介紹了Java Spring Bean的生命周期管理,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

Spring Bean的生命周期管理

一、Spring Bean的生命周期

通過以下方式來指定Bean的初始化和銷毀方法,
當(dāng)Bean為單例時,Bean歸Spring容器管理,Spring容器關(guān)閉,就會調(diào)用Bean的銷毀方法
當(dāng)Bean為多例時,Bean不歸Spring容器管理,Spring容器關(guān)閉,不會調(diào)用Bean的銷毀方法

二、通過@Bean的參數(shù)(initMethod ,destroyMethod)指定Bean的初始化和銷毀方法

1、項目結(jié)構(gòu)

2、Person

public class Person {
    public Person(){
        System.out.println("Person 創(chuàng)建了...");
    }
    public void init(){
        System.out.println("Person 初始化了...");
    }
    public void destroy(){
        System.out.println("Person 被銷毀了...");
    }
}

3、Bean注冊配置類(單實例)

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、測試類

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);
        //關(guān)閉容器
        annotationConfigApplicationContext.close();
    }
}

5、測試結(jié)果

6、Bean注冊配置類(多實例)

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、測試結(jié)果

三、Bean實現(xiàn)接口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 創(chuàng)建了...");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Person 初始化了...");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("Person 被銷毀了...");
    }
}

2、Bean注冊配置類

import com.dashu.bean.Person;
import org.springframework.context.annotation.*;
@Configuration
public class BeanConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}

3、測試結(jié)果

四、通過注解@PostConstruct和@PreDestroy

@PostConstruct:標(biāo)注在Bean的初始化方法上
@PreDestroy:標(biāo)注在Bean的銷毀方法上

1、Person

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Person {
    public Person(){
        System.out.println("Person 創(chuàng)建了...");
    }
    @PostConstruct
    public void init(){
        System.out.println("Person 初始化了...");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("Person 被銷毀了...");
    }
}

2、測試結(jié)果

五、使用接口BeanPostProcessor實現(xiàn)類(后置處理器)

1、項目結(jié)構(gòu)

2、Person

import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class Person {
    public Person(){
        System.out.println("Person 創(chuàng)建了...");
    }
    @PostConstruct
    public void init(){
        System.out.println("Person 初始化了...");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("Person 被銷毀了...");
    }
}

3、Bean注冊配置類

import org.springframework.context.annotation.*;
@Configuration
@ComponentScan({"com.dashu"})
public class BeanConfig {
}

4、BeanPostProcessor實現(xiàn)類(后置處理器)

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;
/**
 * 后置處理器:初始化前后進(jìn)行處理工作
 */
@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、測試結(jié)果

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • java多線程Future和Callable類示例分享

    java多線程Future和Callable類示例分享

    JAVA多線程實現(xiàn)方式主要有三種:繼承Thread類、實現(xiàn)Runnable接口、使用ExecutorService、Callable、Future實現(xiàn)有返回結(jié)果的多線程。其中前兩種方式線程執(zhí)行完后都沒有返回值,只有最后一種是帶返回值的。今天我們就來研究下Future和Callable的實現(xiàn)方法
    2016-01-01
  • Java之Runnable啟動線程的使用方式

    Java之Runnable啟動線程的使用方式

    這篇文章主要介紹了Java之Runnable啟動線程的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • IDEA 創(chuàng)建多級文件夾的操作

    IDEA 創(chuàng)建多級文件夾的操作

    這篇文章主要介紹了IDEA 創(chuàng)建多級文件夾的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • java利用SMB讀取遠(yuǎn)程文件的方法

    java利用SMB讀取遠(yuǎn)程文件的方法

    這篇文章主要為大家詳細(xì)介紹了java利用SMB讀取遠(yuǎn)程文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 如何解決freemarker靜態(tài)化生成html頁面亂碼的問題

    如何解決freemarker靜態(tài)化生成html頁面亂碼的問題

    這篇文章主要介紹了如何解決freemarker靜態(tài)化生成html頁面亂碼的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Java中數(shù)組的定義與使用詳解

    Java中數(shù)組的定義與使用詳解

    這篇文章主要給大家介紹了關(guān)于Java中數(shù)組的定義與使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • Spring事務(wù)Transaction配置的五種注入方式詳解

    Spring事務(wù)Transaction配置的五種注入方式詳解

    這篇文章主要介紹了Spring事務(wù)Transaction配置的五種注入方式詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • 使用IDEA對SpringBoot應(yīng)用進(jìn)行遠(yuǎn)程調(diào)試方式

    使用IDEA對SpringBoot應(yīng)用進(jìn)行遠(yuǎn)程調(diào)試方式

    文章介紹了如何在IDEA中對部署在服務(wù)器上的SpringBoot應(yīng)用進(jìn)行遠(yuǎn)程調(diào)試,通過配置遠(yuǎn)程調(diào)試端口和啟動參數(shù),本地IDEA可以設(shè)置斷點并進(jìn)行調(diào)試
    2025-02-02
  • java分布式流處理組件Producer入門詳解

    java分布式流處理組件Producer入門詳解

    這篇文章主要為大家介紹了java分布式流處理組件Producer入門詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • springboot2中session超時,退到登錄頁面方式

    springboot2中session超時,退到登錄頁面方式

    這篇文章主要介紹了springboot2中session超時,退到登錄頁面方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論

南平市| 固安县| 体育| 黄浦区| 班戈县| 双峰县| 阿城市| 沙坪坝区| 苗栗市| 温州市| 通榆县| 鹤峰县| 嘉鱼县| 临沭县| 迁西县| 环江| 手游| 花莲县| 新河县| 达孜县| 巫山县| 合肥市| 玉树县| 东阿县| 武威市| 上林县| 芒康县| 兴安盟| 呼伦贝尔市| 桃源县| 崇州市| 中西区| 利川市| 金塔县| 景泰县| 齐齐哈尔市| 四会市| 裕民县| 曲靖市| 阿荣旗| 崇信县|