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