spring中的懶加載詳細(xì)解讀
一、懶加載介紹
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)
我們做項目的時候,數(shù)據(jù)量比較大,單表千萬級別的,需要分庫分表,本文主要介紹了Java中ShardingSphere分庫分表實戰(zhàn),感興趣的可以了解一下2021-09-09java.net.MalformedURLException異常的解決方法
下面小編就為大家?guī)硪黄猨ava.net.MalformedURLException異常的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Java中常用解析工具jackson及fastjson的使用
今天給大家?guī)淼氖顷P(guān)于Java解析工具的相關(guān)知識,文章圍繞著jackson及fastjson的使用展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Springboot開發(fā)OAuth2認(rèn)證授權(quán)與資源服務(wù)器操作
這篇文章主要介紹了Springboot開發(fā)OAuth2認(rèn)證授權(quán)與資源服務(wù)器操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot項目網(wǎng)頁加載出現(xiàn)Whitelabel?Error?Page的解決
這篇文章主要介紹了SpringBoot項目網(wǎng)頁加載出現(xiàn)Whitelabel?Error?Page的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11