亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

一、懶加載介紹

1.概念

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

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

2.作用

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

二、環(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>

三、代碼實現(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)境的時候就已經(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)用的時候,再加載創(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)用程序性能和資源利用率的機制。通過延遲加載對象的創(chuàng)建和初始化,可以減少應(yīng)用程序啟動時的負(fù)載和內(nèi)存占用。

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

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

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

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

相關(guān)文章

  • Java中ShardingSphere分庫分表實戰(zhàn)

    Java中ShardingSphere分庫分表實戰(zhàn)

    我們做項目的時候,數(shù)據(jù)量比較大,單表千萬級別的,需要分庫分表,本文主要介紹了Java中ShardingSphere分庫分表實戰(zhàn),感興趣的可以了解一下
    2021-09-09
  • 詳解JAVA設(shè)計模式之代理模式

    詳解JAVA設(shè)計模式之代理模式

    這篇文章主要介紹了JAVA設(shè)計模式之代理模式的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • java.net.MalformedURLException異常的解決方法

    java.net.MalformedURLException異常的解決方法

    下面小編就為大家?guī)硪黄猨ava.net.MalformedURLException異常的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java中常用解析工具jackson及fastjson的使用

    Java中常用解析工具jackson及fastjson的使用

    今天給大家?guī)淼氖顷P(guān)于Java解析工具的相關(guān)知識,文章圍繞著jackson及fastjson的使用展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Java多線程實戰(zhàn)之單例模式與多線程的實例詳解

    Java多線程實戰(zhàn)之單例模式與多線程的實例詳解

    今天小編就為大家分享一篇關(guān)于Java多線程實戰(zhàn)之單例模式與多線程的實例詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 詳解Spring MVC的攔截器與異常處理機制

    詳解Spring MVC的攔截器與異常處理機制

    這篇文章主要為大家詳細(xì)介紹了Spring MVC的攔截器與異常處理機制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 深入解析Java的Hibernate框架中的持久對象

    深入解析Java的Hibernate框架中的持久對象

    Hibernate的持久對象在數(shù)據(jù)庫數(shù)據(jù)操作中有著重要作用,這里我們就來深入解析Java的Hibernate框架中的持久對象,首先必須從理解持久化對象的生命周期開始:
    2016-07-07
  • JVM角度調(diào)試優(yōu)化MyEclipse

    JVM角度調(diào)試優(yōu)化MyEclipse

    這篇文章主要介紹了從JVM角度對MyEclipse進(jìn)行調(diào)試優(yōu)化,為大家分析調(diào)試優(yōu)化MyEclipse的步驟,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Springboot開發(fā)OAuth2認(rèn)證授權(quán)與資源服務(wù)器操作

    Springboot開發(fā)OAuth2認(rèn)證授權(quán)與資源服務(wù)器操作

    這篇文章主要介紹了Springboot開發(fā)OAuth2認(rèn)證授權(quán)與資源服務(wù)器操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringBoot項目網(wǎng)頁加載出現(xiàn)Whitelabel?Error?Page的解決

    SpringBoot項目網(wǎng)頁加載出現(xiàn)Whitelabel?Error?Page的解決

    這篇文章主要介紹了SpringBoot項目網(wǎng)頁加載出現(xiàn)Whitelabel?Error?Page的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評論