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)勢是盡量節(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)文章
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é)
@KafkaListener注解為開發(fā)者提供了一種聲明式的方式來定義消息監(jiān)聽器,本文主要介紹了Spring Kafka中@KafkaListener注解的參數(shù)與使用小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式
這篇文章主要介紹了Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
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)一處理異常詳解
程序在運(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)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
IDEA 2021配置JavaWeb項(xiàng)目超詳細(xì)教程
本文通過圖文并茂的形式給大家介紹IDEA 2021配置JavaWeb項(xiàng)目的過程,內(nèi)容簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08

