欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java深入講解Bean作用域與生命周期

 更新時(shí)間:2022年06月20日 10:38:54   作者:獨(dú)一無二的哈密瓜  
這篇文章主要介紹了淺談Spring中Bean的作用域和生命周期,從創(chuàng)建到消亡的完整過程,例如人從出生到死亡的整個(gè)過程就是一個(gè)生命周期。本文將通過示例為大家詳細(xì)講講,感興趣的可以學(xué)習(xí)一下

1. Bean 的作用域

1.1 觀看案例

現(xiàn)在有一個(gè)公共 Bean

@Component
public class UserBean {
    @Bean
    public User user1() {
        User user = new User();
        user.setId(1);
        user.setName("張三");
        user.setAge(18);
        return user;
    }
}

現(xiàn)在有一個(gè)用戶1, 進(jìn)行了修改的操作

@Controller
public class UserControllerA {
    @Autowired
    private User user1;
    public User getUser1() {
        User user = user1;
        System.out.println("用戶1修改之前: " + user1);
        user.setName("王五");
        return user;
    }
}

現(xiàn)在有用戶2, 進(jìn)行讀取操作

@Controller
public class UserControllerB {
    @Autowired
    private User user1;
    public User getUser1() {
        User user = user1;
        return user;
    }
}

打印用戶1和用戶2公共Bean的值

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        UserControllerA userControllerA = context.getBean("userControllerA",UserControllerA.class);
        User user = userControllerA.getUser1();
        System.out.println("用戶1修改之后: " + user);
        UserControllerB userControllerB = context.getBean("userControllerB",UserControllerB.class);
        User user1 = userControllerB.getUser1();
        System.out.println("用戶2讀取到的: " + user1);
    }

運(yùn)行結(jié)果

原因:

Spring 中 Bean 的作用域默認(rèn)是 singleton 單例模式. 所有人使用的都是同一個(gè)對(duì)象.

1.2 作用域的定義

之前所說的作用域, 是在程序中定義的某個(gè)變量的某個(gè)區(qū)域就叫做作用域.這里的Bean作用域不同.

Bean 的作用域指: Bean在 Spring 整個(gè)框架中的某種行為模式,

比如 singleton 單例作用域, 就表示 Bean 在整個(gè) Spring 中只有一份, 它是全局共享的, 那么當(dāng)其他人修改了這個(gè)值之后, 那么另一個(gè)人讀取到的就是被修改的值.

1.3 Bean 的 6 種作用域

Spring 容器在初始化一個(gè) Bean 的示例時(shí), 同時(shí)會(huì)指定該實(shí)例的作用域.

Spring 有 6 種作用域.

  • singleton: 單例作用域
  • prototype: 原型作用域 (多例作用域)
  • request: 請(qǐng)求作用域
  • session: 回話作用域
  • application: 全局作用域
  • websocket: HTTP WebSocket 作用域

3~6 這四種是 Spring MVC 中的值.

1.4 如何設(shè)置 Bean 的作用域

@Scope 既可以用于方法注解, 也可以用于類注解

① @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

② @Scope(“prototype”)

注意事項(xiàng):

如果是 @Bean + @Scope , 這里的@Scope 就不能寫成類注解的方式了.

2. Bean 的生命周期

2.1 Bean 的執(zhí)行流程

  • 啟動(dòng) Spring 容器
  • 加載 Spring 配置文件
  • 加載 Spring 配置文件中的 Bean, 或根據(jù)配置文件中的組件根路徑, 進(jìn)行 Bean 對(duì)象的掃描(掃描@Controller…)
  • Bean 注冊(cè)到 Spring 中
  • 執(zhí)行完畢,執(zhí)行銷毀操作

2.2 Bean 生命周期

Bean 的生命周期分為 5 大部分:

實(shí)例化 Bean (為Bean分配內(nèi)存空間)

設(shè)置屬性 (Bean 進(jìn)行初始化注入)

Bean 初始化 (一系列準(zhǔn)備工作)

① 各種通知 (xxxAware)

② 執(zhí)行初始化前置方法 BeanPostProcessor

③ 執(zhí)行 @PostConstruct 初始化方法

④ 執(zhí)行 init-mehtod 初始化方法

⑤ 執(zhí)行初始化后置方法 BeanPostProcessor

使用 Bean

銷毀 Bean

  • ① 執(zhí)行銷毀前方法 @PreDestory
  • ② 如果有接口 DisposableBean, 執(zhí)行實(shí)現(xiàn)方法
  • ③ 執(zhí)行銷毀前的方法 destory-method

2.3 生命周期演示代碼

主要代碼

package com.wwzz.util;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class BeanLifeComponent implements BeanNameAware {
    @Override
    public void setBeanName(String s) {
        System.out.println("執(zhí)行通知: BeanNameAware " + s);
    }
    @PostConstruct
    public void postConstruct(){
        System.out.println("執(zhí)行 @PostConstruct");
    }
    public void init() {
        System.out.println("執(zhí)行 init-method");
    }
    @PreDestroy
    public void preDestroy(){
        System.out.println("執(zhí)行 @PreDestroy");
    }
    public void destroy() {
        System.out.println("執(zhí)行 destroy");
    }
}

xml 的配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:content="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置 Spring 掃描的根路徑(此根路徑下的所有 Spring 存對(duì)象的注解才能生效) --> <content:component-scan base-package="com.wwzz"></content:component-scan> <beans> <bean id="beanLifeComponent" class="com.wwzz.util.BeanLifeComponent" init-method="init" destroy-method="destroy"></bean> </beans></beans><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:content="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置 Spring 掃描的根路徑(此根路徑下的所有 Spring 存對(duì)象的注解才能生效) -->
    <content:component-scan base-package="com.wwzz"></content:component-scan>
    <beans>
        <bean id="beanLifeComponent" class="com.wwzz.util.BeanLifeComponent" init-method="init" destroy-method="destroy"></bean>
    </beans>
</beans>

測(cè)試運(yùn)行

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        BeanLifeComponent beanLifeComponent = context.getBean("beanLifeComponent",BeanLifeComponent.class);
        context.destroy();
    }

查看結(jié)果

到此這篇關(guān)于Java深入講解Bean作用域與生命周期的文章就介紹到這了,更多相關(guān)Bean作用域與生命周期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • nacos在liunx系統(tǒng)中啟動(dòng)成功瀏覽器卻訪問不了的解決方法

    nacos在liunx系統(tǒng)中啟動(dòng)成功瀏覽器卻訪問不了的解決方法

    在linux下搭建nacos,現(xiàn)在想要啟動(dòng),訪問nacos頁面,訪問不了,所以本文小編將給大家介紹nacos在liunx系統(tǒng)中啟動(dòng)成功,瀏覽器卻訪問不了?全面的解決辦法,需要的朋友可以參考下
    2023-09-09
  • Java用freemarker導(dǎo)出word實(shí)用示例

    Java用freemarker導(dǎo)出word實(shí)用示例

    本篇文章主要介紹了Java用freemarker導(dǎo)出word實(shí)用示例,使用freemarker的模板來實(shí)現(xiàn)功能,有需要的可以了解一下。
    2016-11-11
  • java實(shí)現(xiàn)單鏈表、雙向鏈表

    java實(shí)現(xiàn)單鏈表、雙向鏈表

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)單鏈表、雙向鏈表的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • springboot運(yùn)行時(shí)新增/更新外部接口的實(shí)現(xiàn)方法

    springboot運(yùn)行時(shí)新增/更新外部接口的實(shí)現(xiàn)方法

    這篇文章主要介紹了springboot運(yùn)行時(shí)新增/更新外部接口的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Java中Synchronized鎖的使用和原理詳解

    Java中Synchronized鎖的使用和原理詳解

    這篇文章主要介紹了Java中Synchronized鎖的使用和原理詳解,synchronized是?Java?內(nèi)置的關(guān)鍵字,它提供了一種獨(dú)占的加鎖方式,synchronized的獲取和釋放鎖由JVM實(shí)現(xiàn),用戶不需要顯示的釋放鎖,非常方便,需要的朋友可以參考下
    2023-07-07
  • 一篇文章帶你入門java多線程

    一篇文章帶你入門java多線程

    這篇文章主要介紹了java多線程編程實(shí)例,分享了幾則多線程的實(shí)例代碼,具有一定參考價(jià)值,加深多線程編程的理解還是很有幫助的,需要的朋友可以參考下
    2021-08-08
  • Java利用Reflect實(shí)現(xiàn)封裝Excel導(dǎo)出工具類

    Java利用Reflect實(shí)現(xiàn)封裝Excel導(dǎo)出工具類

    這篇文章主要為大家詳細(xì)介紹了Java如何利用Reflect實(shí)現(xiàn)封裝Excel導(dǎo)出工具類,文中的實(shí)現(xiàn)方法講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2022-11-11
  • SpringBoot整合SpringSecurity認(rèn)證與授權(quán)

    SpringBoot整合SpringSecurity認(rèn)證與授權(quán)

    在項(xiàng)目開發(fā)中,權(quán)限認(rèn)證是很重要的,尤其是一些管理類的系統(tǒng),對(duì)于權(quán)限要求更為嚴(yán)格,本文主要介紹了SpringBoot整合SpringSecurity認(rèn)證與授權(quán),感興趣的可以了解一下
    2023-11-11
  • java實(shí)現(xiàn)簡單日期計(jì)算功能

    java實(shí)現(xiàn)簡單日期計(jì)算功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單日期計(jì)算功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 淺談springBoot注解大全

    淺談springBoot注解大全

    本篇文章主要介紹了淺談springBoot注解大全,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03

最新評(píng)論