Spring通過(guò)@Lazy解決構(gòu)造方法形式的循環(huán)依賴(lài)問(wèn)題
一、定義2個(gè)循環(huán)依賴(lài)的類(lèi)
package cn.edu.tju.domain2; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component public class A { private final B b; public B getB() { return b; } @Lazy public A(B b){ this.b = b; //System.out.println(b); } }
package cn.edu.tju.domain2; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component public class B { private final A a; public A getA() { return a; } @Lazy public B(A a){ this.a =a; //System.out.println(a); } }
二、定義配置文件(spring09.xml):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="cn.edu.tju.domain2"/> </beans>
三、定義測(cè)試類(lèi):
package cn.edu.tju; import cn.edu.tju.domain.Husband; import cn.edu.tju.domain2.A; import cn.edu.tju.domain2.B; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledConnection; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ConfigurationClassPostProcessor; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.sql.SQLException; public class Test09 { public static void main(String[] args) throws Exception { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("spring09.xml"); A a = ctx.getBean("a", A.class); B b = ctx.getBean("b", B.class); System.out.println(a.getClass().getName()); System.out.println(a.getB().getA() == a); System.out.println(a.getB().getClass().getName()); System.out.println(b.getA().getClass().getName()); } }
四、執(zhí)行結(jié)果:
以上就是Spring通過(guò)@Lazy解決構(gòu)造方法形式的循環(huán)依賴(lài)問(wèn)題的詳細(xì)內(nèi)容,更多關(guān)于Spring @Lazy解決循環(huán)依賴(lài)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JAVA正則表達(dá)式校驗(yàn)qq號(hào)碼的方法
Java作為一種開(kāi)發(fā)語(yǔ)言,有許多值得推薦的地方,但是它一直以來(lái)沒(méi)有自帶對(duì)正則表達(dá)式的支持。下面小編給大家?guī)?lái)了JAVA正則表達(dá)式校驗(yàn)qq號(hào)碼的方法,需要的朋友參考下吧2018-04-04Java IText異常NoClassDefFoundError: org/bouncycastle
在使用Java進(jìn)行PDF文檔操作時(shí),iText是一個(gè)非常強(qiáng)大的庫(kù),然而,在實(shí)際開(kāi)發(fā)過(guò)程中,可能會(huì)遇到一些異常情況,其中之一就是??NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable??,本文將探討這個(gè)錯(cuò)誤的原因及其解決方案,需要的朋友可以參考下2025-02-02java自定義類(lèi)加載器如何實(shí)現(xiàn)類(lèi)隔離
這篇文章主要介紹了java自定義類(lèi)加載器如何實(shí)現(xiàn)類(lèi)隔離問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11java 抽象類(lèi)與接口的區(qū)別總結(jié)
這篇文章主要介紹了java 抽象類(lèi)與接口的區(qū)別總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-02-02解決IDEA修改 .vmoptions 文件后導(dǎo)致無(wú)法啟動(dòng)的問(wèn)題
這篇文章主要介紹了解決IDEA修改 .vmoptions 文件后導(dǎo)致無(wú)法啟動(dòng)的問(wèn)題,需要的朋友可以參考下2020-12-12使用自定義注解進(jìn)行restful請(qǐng)求參數(shù)的校驗(yàn)方式
這篇文章主要介紹了使用自定義注解進(jìn)行restful請(qǐng)求參數(shù)的校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10SpringRetry重試機(jī)制之@Retryable注解與重試策略詳解
本文將詳細(xì)介紹SpringRetry的重試機(jī)制,特別是@Retryable注解的使用及各種重試策略的配置,幫助開(kāi)發(fā)者構(gòu)建更加健壯的應(yīng)用程序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04java實(shí)現(xiàn)同步的幾種方式(示例詳解)
這篇文章主要介紹了java實(shí)現(xiàn)同步的幾種方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12Java實(shí)現(xiàn)word轉(zhuǎn)pdf并在關(guān)鍵字位置插入圖片
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)word轉(zhuǎn)pdf,并在word中關(guān)鍵字位置插入圖片,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11