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

spring中的懶加載詳細(xì)解讀

 更新時(shí)間:2023年10月12日 10:12:49   作者:RayBreslin  
這篇文章主要介紹了spring中的懶加載詳細(xì)解讀,如果某個(gè)Bean再程序運(yùn)行周期中都可能不會(huì)被適用,那么可以設(shè)定該Bean為懶加載,優(yōu)勢是盡量節(jié)省了服務(wù)器的資源,缺點(diǎn)是可能會(huì)導(dǎo)致某個(gè)相應(yīng)的時(shí)間增加,需要的朋友可以參考下

一、懶加載介紹

1.概念

Spring容器會(huì)在創(chuàng)建容器時(shí)提前初始化Singleton作用域的bean,即在創(chuàng)建環(huán)境ApplicationContext的時(shí)候,單例作用域的Bean就會(huì)被實(shí)例化。注意:如果是prototype作用域的bean,則其是在調(diào)用該bean的時(shí)候創(chuàng)建的(已經(jīng)驗(yàn)證)

但是如果Bean被標(biāo)注了lazy-init="true",則該Bean只有在其被需要的時(shí)候才會(huì)被初始化。

2.作用

如果某個(gè)Bean再程序運(yùn)行周期中都可能不會(huì)被適用,那么可以設(shè)定該Bean為懶加載。優(yōu)勢是盡量節(jié)省了服務(wù)器的資源,缺點(diǎn)是可能會(huì)導(dǎo)致某個(gè)相應(yīng)的時(shí)間增加。

二、環(huán)境

1.pom.xml文件

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.7.RELEASE</version>
</dependency>
<!--單元測試-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

2.Spring.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"
       default-autowire="byName"
       xmlns:context="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 http://www.springframework.org/schema/context/spring-context-4.2.xsd">
</beans>

三、代碼實(shí)現(xiàn)

1.創(chuàng)建bean

package com.spring.ioc;
/**
 * Created by Administrator on 2019/10/26.
 */
public class Bean1 {
    public Bean1() {
        System.out.println("Bean1 has been created ");
    }
}

2.交給spring管理:對于單例模式,非懶加載

<?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:c="http://www.springframework.org/schema/c"
       xmlns:p ="http://www.springframework.org/schema/p"
       default-autowire="byName"
       xmlns:context="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 http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <bean class="com.spring.ioc.Bean1" id="bean1" />
</beans>

-》測試

package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {
    @Test
    public void test(){
        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        System.out.println("context已經(jīng)被創(chuàng)建");
        Bean1 bean1=context.getBean("bean1",Bean1.class);
        System.out.println("bean1 = " + bean1);
    }
}

結(jié)果,Bean在加載spring環(huán)境的時(shí)候就已經(jīng)被加載了

Bean1 has been created 
context已經(jīng)被創(chuàng)建
bean1 = com.spring.ioc.Bean1@7d0587f1

3.交給spring管理:對于單例模式,懶加載

<?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:c="http://www.springframework.org/schema/c"
       xmlns:p ="http://www.springframework.org/schema/p"
       default-autowire="byName"
       xmlns:context="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 http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!--<bean class="com.spring.ioc.Bean1" id="bean1" />-->
    <bean class="com.spring.ioc.Bean1" id="bean1" lazy-init="true"/>
</beans>

-》測試

package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {
    @Test
    public void test(){
        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        System.out.println("context已經(jīng)被創(chuàng)建");
        Bean1 bean1=context.getBean("bean1",Bean1.class);
        System.out.println("bean1 = " + bean1);
    }
}

結(jié)果,Bean是在調(diào)用的時(shí)候,再加載創(chuàng)建的。

context已經(jīng)被創(chuàng)建
Bean1 has been created 

4.將spring.xml下所有bean統(tǒng)一都設(shè)置為懶加載

<?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:c="http://www.springframework.org/schema/c"
       xmlns:p ="http://www.springframework.org/schema/p"
       default-autowire="byName"
       xmlns:context="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 http://www.springframework.org/schema/context/spring-context-4.2.xsd"
       default-lazy-init="true"
>
    <!--<bean class="com.spring.ioc.Bean1" id="bean1" />-->
    <bean class="com.spring.ioc.Bean1" id="bean1" lazy-init="true"/>
</beans>

總結(jié)

Spring 懶加載是一種優(yōu)化應(yīng)用程序性能和資源利用率的機(jī)制。通過延遲加載對象的創(chuàng)建和初始化,可以減少應(yīng)用程序啟動(dòng)時(shí)的負(fù)載和內(nèi)存占用。

懶加載可以通過使用@Lazy注解、配置文件中的lazy-init屬性或編程方式來實(shí)現(xiàn)。在處理大量對象或?qū)ο蟪跏蓟^為耗時(shí)的情況下,懶加載可以顯著提高應(yīng)用程序的響應(yīng)速度和效率。

然而,需要注意的是,懶加載可能會(huì)導(dǎo)致一些副作用,如延遲了對象的初始化和依賴注入,可能會(huì)在運(yùn)行時(shí)出現(xiàn)錯(cuò)誤。

因此,在使用懶加載時(shí),需要仔細(xì)考慮對象的依賴關(guān)系和初始化順序,以確保應(yīng)用程序的正確性和穩(wěn)定性。

到此這篇關(guān)于spring中的懶加載詳細(xì)解讀的文章就介紹到這了,更多相關(guān)spring懶加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot中的雙數(shù)據(jù)源配置過程

    SpringBoot中的雙數(shù)據(jù)源配置過程

    文章介紹了如何配置Oracle和PostgreSQL數(shù)據(jù)源,并使用JdbcTemplate進(jìn)行數(shù)據(jù)庫操作,重點(diǎn)包括數(shù)據(jù)源配置、JdbcTemplate配置以及不同數(shù)據(jù)源的包路徑設(shè)置
    2026-01-01
  • java8 stream自定義分組求和并排序的實(shí)現(xiàn)

    java8 stream自定義分組求和并排序的實(shí)現(xiàn)

    這篇文章主要介紹了java8 stream自定義分組求和并排序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Spring Kafka中@KafkaListener注解的參數(shù)與使用小結(jié)

    Spring Kafka中@KafkaListener注解的參數(shù)與使用小結(jié)

    @KafkaListener注解為開發(fā)者提供了一種聲明式的方式來定義消息監(jiān)聽器,本文主要介紹了Spring Kafka中@KafkaListener注解的參數(shù)與使用小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式

    Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式

    這篇文章主要介紹了Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java項(xiàng)目命名規(guī)范參考

    Java項(xiàng)目命名規(guī)范參考

    在實(shí)際項(xiàng)目開發(fā)中,命名規(guī)范的遵守可以提高代碼的可讀性和可維護(hù)性,本文就來介紹一下Java項(xiàng)目命名規(guī)范參考,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • SpringBoot實(shí)現(xiàn)第一次啟動(dòng)時(shí)自動(dòng)初始化數(shù)據(jù)庫的方法

    SpringBoot實(shí)現(xiàn)第一次啟動(dòng)時(shí)自動(dòng)初始化數(shù)據(jù)庫的方法

    本文主要介紹了SpringBoot實(shí)現(xiàn)第一次啟動(dòng)時(shí)自動(dòng)初始化數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 你必須得會(huì)的SpringBoot全局統(tǒng)一處理異常詳解

    你必須得會(huì)的SpringBoot全局統(tǒng)一處理異常詳解

    程序在運(yùn)行的過程中,不可避免會(huì)產(chǎn)生各種各樣的錯(cuò)誤,這個(gè)時(shí)候就需要進(jìn)行異常處理,本文主要為大家介紹了SpringBoot實(shí)現(xiàn)全局統(tǒng)一處理異常的方法,需要的可以參考一下
    2023-06-06
  • java中Timer定時(shí)器的使用和啟動(dòng)方式

    java中Timer定時(shí)器的使用和啟動(dòng)方式

    這篇文章主要介紹了java中Timer定時(shí)器的使用和啟動(dòng)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • IDEA 2021配置JavaWeb項(xiàng)目超詳細(xì)教程

    IDEA 2021配置JavaWeb項(xiàng)目超詳細(xì)教程

    本文通過圖文并茂的形式給大家介紹IDEA 2021配置JavaWeb項(xiàng)目的過程,內(nèi)容簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-08-08
  • Java的Shiro框架認(rèn)證流程詳解

    Java的Shiro框架認(rèn)證流程詳解

    這篇文章主要介紹了Java的Shiro框架認(rèn)證流程詳解,Shiro 是一個(gè)功能強(qiáng)大和易于使用的安全框架,為開發(fā)人員提供一個(gè)直觀而全面的解決方案的認(rèn)證,授權(quán),加密,會(huì)話管理四大功能,需要的朋友可以參考下
    2024-01-01

最新評論

静安区| 湖北省| 阜康市| 江都市| 错那县| 德昌县| 新田县| 资兴市| 辰溪县| 遂川县| 扶余县| 通化市| 武乡县| 亚东县| 德安县| 天长市| 永靖县| 墨竹工卡县| 吉木萨尔县| 平安县| 潜江市| 尤溪县| 宣威市| 辽中县| 大新县| 资溪县| 斗六市| 崇左市| 都昌县| 双桥区| 伊通| 彭泽县| 永仁县| 广平县| 永丰县| 盐源县| 牙克石市| 陇川县| 靖远县| 汾西县| 海盐县|