" />

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

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

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

Spring Bean的生命周期管理

一、Spring Bean的生命周期

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

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

1、項(xiàng)目結(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注冊(cè)配置類(單實(shí)例)

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、測(cè)試類

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

6、Bean注冊(cè)配置類(多實(shí)例)

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

三、Bean實(shí)現(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注冊(cè)配置類

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

3、測(cè)試結(jié)果

四、通過(guò)注解@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、測(cè)試結(jié)果

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

1、項(xiàng)目結(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注冊(cè)配置類

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

4、BeanPostProcessor實(shí)現(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、測(cè)試結(jié)果

總結(jié)

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

相關(guān)文章

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

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

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

    Java之Runnable啟動(dòng)線程的使用方式

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    springboot2中session超時(shí),退到登錄頁(yè)面方式

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

最新評(píng)論

星子县| 阿巴嘎旗| 襄汾县| 苗栗县| 伊金霍洛旗| 辽源市| 洪洞县| 揭西县| 襄城县| 巫山县| 河西区| 株洲市| 奉节县| 忻州市| 潮安县| 荔浦县| 宜川县| 红河县| 昌图县| 腾冲县| 龙川县| 安西县| 八宿县| 溧阳市| 武隆县| 宁波市| 泗阳县| 寿阳县| 新绛县| 蒙自县| 穆棱市| 扎赉特旗| 安泽县| 鸡西市| 绥化市| 河西区| 商洛市| 双柏县| 邢台市| 兴安县| 威信县|