Spring中的@Lazy注解用法實(shí)例
一、@Lazy注解
1、@Lazy注解作用
lazy 翻譯過(guò)來(lái)是"懶惰的"
@Lazy(懶加載):該注解用于惰性加載初始化標(biāo)注的類、方法和參數(shù)。
在Spring中常用于單實(shí)例Bean對(duì)象的創(chuàng)建和使用;
單實(shí)例Bean懶加載:容器啟動(dòng)后不創(chuàng)建對(duì)象,而是在第一次獲取Bean創(chuàng)建對(duì)象時(shí),初始化。

2、@Lazy
可標(biāo)注在類、方法、構(gòu)造方法、參數(shù)、字段上
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {
/**
* Whether lazy initialization should occur.
*/
boolean value() default true;
}二、@Lazy案例
1、項(xiàng)目結(jié)構(gòu)

2、Persion
package com.dashu.bean;
public class Persion {
public Persion(String name, int age) {
this.name = name;
this.age = age;
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Persion{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}3、Bean注冊(cè)配置類
package com.dashu.config;
import com.dashu.bean.Persion;
import org.springframework.context.annotation.*;
/**
* @Configuration 注解:告訴Spring這是一個(gè)配置類
*
* 配置類 == 配置文件(beans.xml文件)
*
*/
@Configuration
public class BeanConfig {
@Lazy
@Bean
public Persion persion(){
System.out.println("初始化Persion...");
return new Persion("張三",20);
}
}4、測(cè)試類
package com.dashu;
import com.dashu.bean.Persion;
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
/**
* 只在第一次獲取Bean時(shí),初始化。之后的獲取都是同一個(gè)對(duì)象
*/
Persion persion1 = (Persion) annotationConfigApplicationContext.getBean("persion");
Persion persion2 = annotationConfigApplicationContext.getBean(Persion.class);
System.out.println(persion1 == persion2);
}
}5、測(cè)試結(jié)果

到此這篇關(guān)于Spring中的@Lazy注解用法實(shí)例的文章就介紹到這了,更多相關(guān)Spring的@Lazy內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 配置log4j日志文件路徑 (附-獲取當(dāng)前類路徑的多種操作)
這篇文章主要介紹了Java 配置log4j日志文件路徑 (附-獲取當(dāng)前類路徑的多種操作),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Java可以如何實(shí)現(xiàn)文件變動(dòng)的監(jiān)聽(tīng)的示例
本篇文章主要介紹了Java可以如何實(shí)現(xiàn)文件變動(dòng)的監(jiān)聽(tīng)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Springboot如何設(shè)置多數(shù)據(jù)源,隨時(shí)切換
這篇文章主要介紹了Springboot如何設(shè)置多數(shù)據(jù)源,隨時(shí)切換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
如何在Spring Boot應(yīng)用程序中配置了兩個(gè)不同的SOAP Web服務(wù)端點(diǎn)
這篇文章主要介紹了如何在Spring Boot應(yīng)用程序中配置了兩個(gè)不同的SOAP Web服務(wù)端點(diǎn),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
Java?將PDF轉(zhuǎn)為HTML時(shí)保存到流的方法和步驟
本文介紹如何通過(guò)Java后端程序代碼將PDF文件轉(zhuǎn)為HTML,并將轉(zhuǎn)換后的HTML文件保存到流,下面是實(shí)現(xiàn)轉(zhuǎn)換的方法和步驟,感興趣的朋友一起看看吧2022-01-01
一文探究ArrayBlockQueue函數(shù)及應(yīng)用場(chǎng)景
這篇文章主要為大家介紹了一文探究ArrayBlockQueue函數(shù)及應(yīng)用場(chǎng)景,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
java使用正則表達(dá)校驗(yàn)手機(jī)號(hào)碼示例(手機(jī)號(hào)碼正則)
這篇文章主要介紹了java使用正則表達(dá)校驗(yàn)手機(jī)號(hào)碼示例,可校驗(yàn)三個(gè)號(hào)碼段:13*、15*、18*,大家根據(jù)自己的需要增加自己的號(hào)碼段就可以了2014-03-03
Java中excel表數(shù)據(jù)的批量導(dǎo)入方法
這篇文章主要為大家詳細(xì)介紹了Java中excel表數(shù)據(jù)的批量導(dǎo)入方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

