spring中的懶加載詳細(xì)解讀
一、懶加載介紹
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)勢(shì)是盡量節(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> <!--單元測(cè)試--> <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管理:對(duì)于單例模式,非懶加載
<?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>
-》測(cè)試
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管理:對(duì)于單例模式,懶加載
<?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>
-》測(cè)試
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ī)制。通過延遲加載對(duì)象的創(chuàng)建和初始化,可以減少應(yīng)用程序啟動(dòng)時(shí)的負(fù)載和內(nèi)存占用。
懶加載可以通過使用@Lazy注解、配置文件中的lazy-init屬性或編程方式來實(shí)現(xiàn)。在處理大量對(duì)象或?qū)ο蟪跏蓟^為耗時(shí)的情況下,懶加載可以顯著提高應(yīng)用程序的響應(yīng)速度和效率。
然而,需要注意的是,懶加載可能會(huì)導(dǎo)致一些副作用,如延遲了對(duì)象的初始化和依賴注入,可能會(huì)在運(yùn)行時(shí)出現(xiàn)錯(cuò)誤。
因此,在使用懶加載時(shí),需要仔細(xì)考慮對(duì)象的依賴關(guān)系和初始化順序,以確保應(yīng)用程序的正確性和穩(wěn)定性。
到此這篇關(guān)于spring中的懶加載詳細(xì)解讀的文章就介紹到這了,更多相關(guān)spring懶加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中ShardingSphere分庫分表實(shí)戰(zhàn)
我們做項(xiàng)目的時(shí)候,數(shù)據(jù)量比較大,單表千萬級(jí)別的,需要分庫分表,本文主要介紹了Java中ShardingSphere分庫分表實(shí)戰(zhàn),感興趣的可以了解一下2021-09-09java.net.MalformedURLException異常的解決方法
下面小編就為大家?guī)硪黄猨ava.net.MalformedURLException異常的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Java中常用解析工具jackson及fastjson的使用
今天給大家?guī)淼氖顷P(guān)于Java解析工具的相關(guān)知識(shí),文章圍繞著jackson及fastjson的使用展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Java多線程實(shí)戰(zhàn)之單例模式與多線程的實(shí)例詳解
今天小編就為大家分享一篇關(guān)于Java多線程實(shí)戰(zhàn)之單例模式與多線程的實(shí)例詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02深入解析Java的Hibernate框架中的持久對(duì)象
Hibernate的持久對(duì)象在數(shù)據(jù)庫數(shù)據(jù)操作中有著重要作用,這里我們就來深入解析Java的Hibernate框架中的持久對(duì)象,首先必須從理解持久化對(duì)象的生命周期開始:2016-07-07Springboot開發(fā)OAuth2認(rèn)證授權(quán)與資源服務(wù)器操作
這篇文章主要介紹了Springboot開發(fā)OAuth2認(rèn)證授權(quán)與資源服務(wù)器操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot項(xiàng)目網(wǎng)頁加載出現(xiàn)Whitelabel?Error?Page的解決
這篇文章主要介紹了SpringBoot項(xiàng)目網(wǎng)頁加載出現(xiàn)Whitelabel?Error?Page的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11