springbean初始化流程圖 Spring如何解決循環(huán)依賴的問題?
Spring如何解決循環(huán)依賴的問題?(一)SpringIOC容器---對(duì)象停止循環(huán)依賴1.什么是循環(huán)依賴?what?(1)循環(huán)依賴--dstrok循環(huán)摘錄。---a8即2個(gè)或以上bean一起所屬對(duì)方,
Spring如何解決循環(huán)依賴的問題?
(一)SpringIOC容器---對(duì)象停止循環(huán)依賴
1.什么是循環(huán)依賴?what?
(1)循環(huán)依賴--dstrok循環(huán)摘錄。---a8即2個(gè)或以上bean一起所屬對(duì)方,最終形成閉環(huán)。
eg:A依戀B,B依戀C,C又依賴性太強(qiáng)A?!颈M量:這里又不是函數(shù)的循環(huán)動(dòng)態(tài)創(chuàng)建【是個(gè)死循環(huán),如果有畫上句號(hào)條件】,是對(duì)象相互依賴關(guān)系】
2.Spring中重復(fù)運(yùn)行依戀的場景?where?
①:構(gòu)造器的循環(huán)依賴。【這個(gè)Spring能解決不了】
StudentA有參構(gòu)造是StudentB。StudentB的有參構(gòu)造是StudentC,StudentC的有參構(gòu)造是StudentA,這樣的話就有一種了一個(gè)循環(huán)依賴的情況,
我們都把這三個(gè)Bean丟給Spring管理,用長有參構(gòu)造構(gòu)造函數(shù)
stateclassStudentA{
privateStudentBstudentB
statevoidsetStudentB(StudentBstudentB){
studentB
}
publicStudentA(){
}
welfareStudentA(StudentBstudentB){
studentB
}
}
[java]viewplain剪切粘貼
employeeclassStudentB{
privateStudentCstudentC
statevoidsetStudentC(StudentCstudentC){
studentC
}
stateStudentB(){
}
employeeStudentB(StudentCstudentC){
studentC
}
}
[java]viewplain全部復(fù)制
publicclassStudentC{
privateStudentAstudentA
statevoidsetStudentA(StudentAstudentA){
studentA
}
employeeStudentC(){
}
welfareStudentC(StudentAstudentA){
studentA
}
}
[html]viewplain剪切粘貼
ltbeanida
ltconstructor-argindex0refbgtlt/constructor-arggt
lt/beangt
ltbeanidb
ltconstructor-argindex0refcgtlt/constructor-arggt
lt/beangt
ltbeanidc
ltconstructor-argindex0refagtlt/constructor-arggt
lt/beangt
下面是測試類:
[java]viewplain全部復(fù)制
welfareclassTest{
welfarestaticvoidmain(String[]args){
ApplicationContextcontextnewClassPathXmlApplicationContext(com/zfx/student/applicationContext.xml)
((a
spring bean class作用?
1.作用
Bean標(biāo)簽是作用于配置對(duì)象,讓spring來創(chuàng)建戰(zhàn)隊(duì)的。
默認(rèn)情況下它調(diào)用的是類中的無參構(gòu)造函數(shù)。假如沒有無參構(gòu)造函數(shù)則又不能創(chuàng)建成功。
2.屬性
id:給對(duì)象在容器中提供給一個(gè)同樣標(biāo)志,主要用于聲望兌換對(duì)象。
class:指定你類的全時(shí)間限制name屬性,主要是用于反射創(chuàng)建戰(zhàn)隊(duì)對(duì)象。默認(rèn)情況下動(dòng)態(tài)創(chuàng)建無參構(gòu)造函數(shù)。
scope:更改對(duì)象的作用范圍。
二、Bean標(biāo)簽的屬性
Bean標(biāo)簽中的scope屬性,用于具體描述bean的作用域。取值如下:
singleton:默認(rèn)值,單例的。代表在Spring Ioc容器中只能一個(gè)Bean實(shí)例。
prototype:多例的。每當(dāng)從Spring容器中某些時(shí)都會(huì)直接返回個(gè)新的實(shí)例。
request:WEB項(xiàng)目中,Spring創(chuàng)建角色一個(gè)Bean的對(duì)象,將對(duì)象轉(zhuǎn)存到request域中。
session:WEB項(xiàng)目中,Spring創(chuàng)建一個(gè)Bean的對(duì)象,將對(duì)象存進(jìn)到session域中。
globalsession:WEB項(xiàng)目中,作用于集群環(huán)境(Porlet)的會(huì)話范圍(全局會(huì)話范圍)。假如沒有集群環(huán)境(Portlet)這樣globalSession等同于session。
init-method:指定類中的初始化方法名稱。
destroy-method:委托類中全部銷毀方法名稱。
三、Bean標(biāo)簽的scope屬性
scope屬性t只能說明
單例singletont對(duì)象只創(chuàng)建家族兩次,容器創(chuàng)建時(shí)創(chuàng)建角色
原型prototypet每動(dòng)態(tài)創(chuàng)建兩次就創(chuàng)建戰(zhàn)隊(duì)一個(gè)新的對(duì)象,對(duì)象全局函數(shù)時(shí)創(chuàng)建戰(zhàn)隊(duì)
requestt你每次HTTP跪請(qǐng)時(shí)創(chuàng)建一個(gè)實(shí)例
sessiont對(duì)于每個(gè)HTPPsession創(chuàng)建角色一個(gè)實(shí)例
1.測試scope“singleton”
singleton:默認(rèn)值,單例的。代表在Spring Ioc容器中只能一個(gè)Bean實(shí)例。
lt?xmlversion#341.0#34encoding#34UTF-8#34?r26
ltbeansxmlns##34
txmlns:xsi##34
txsi:schemaLocation##34rlm
tltbeanid#34p#34class##34cgtlt/beangt
lt/beansgt
package
importorg.junit.Test
import
import
import
/**
*類只能證明:
*tt測試Bean標(biāo)簽的scope屬性
*@guoqianliang1998.
*/
publicclassDemo{
tpublicvoidtestScope(){
ttApplicationContextacfutureClassPathXmlApplicationContext(#34applicationContext.xml#34)
ttPersonp1(Person)(#34p#34)
ttPersonp2(Person)(#34p#34)
ttPersonp3(Person)(#34p#34)
(p1)
(p2)
(p3)
t
“prototype”
prototype:多例的。每一道從Spring容器中查看時(shí)都會(huì)前往一個(gè)新的實(shí)例。
lt?xmlversion#341.0#34encoding#34UTF-8#34?a8
ltbeansxmlns##34
txmlns:xsi##34
txsi:schemaLocation##34rlm
tltbeanid#34p#34class##34scope#34prototype#34gtlt/beangt
lt/beansgt
package
importorg.junit.Test
import
import
import
/**
*類只能證明:
*tt測試Bean標(biāo)簽的scope屬性
*@guoqianliang1998.
*/
welfareclassDemo{
tpublicvoidtestScope(){
ttApplicationContextacnewClassPathXmlApplicationContext(#34applicationContext.xml#34)
ttPersonp1(Person)(#34p#34)
ttPersonp2(Person)(#34p#34)
ttPersonp3(Person)(#34p#34)
(p1)
(p2)
(p3)
四、scope#34singleton#34受到的線程安全問題
單例模式下,在對(duì)象實(shí)體類的成員位置參與刪削改,很有可能會(huì)演變成線程安全問題。
而寫在函數(shù)內(nèi)部(淺表位置)則絕對(duì)不會(huì)直接導(dǎo)致線程安全問題,這是而且對(duì)象在這一瞬間調(diào)用函數(shù),都會(huì)在??臻g中開劈空間,函數(shù)執(zhí)行完就后退,生命周期短。
package
import
import
/**
*類只能說明:
*tt實(shí)體類Person
*@guoqianliang1998.
*/
welfareclassPerson{
tprivateStringname
t//如果不是對(duì)list參與增刪改,很可能誘發(fā)線程安全問題。
tpublicListltStringgt list new ArrayListltStringgt()
t//無參構(gòu)造
tpublicPerson(){
t}
t//有參構(gòu)造
tpublicPerson(String name){
name
t}
tpublicvoidtalks(){
(#34talk方法...#34)
t}
}