Back to Javatutorial

Spring探秘之循环依赖的解决(一):理论基石

docs/Spring全家桶/Spring源码分析/Spring重要机制探秘/Spring探秘之循环依赖的解决(一):理论基石.md

1.0.029.5 KB
Original Source

1. ʲôѭ

spring עʱܻ໥ע

@Service
public class Service1 {
    @Autowired
    private Service2 service2;

}

@Service
public class Service2 {
    @Autowired
    private Service1 service1;

}

ϴ룬 Service1 ͨ @Autowird ע Service2 Service2 ͨ @Autowird ע Service1໥עͽѭ

2. ѭʲô

ʵϣ ABBҲAjava ȫֵ֧ģ

/**
 * ׼service1
 */
public class Service1 {
    private Service2 service2;

    public void setService2(Service2 service2) {
        this.service2 = service2;
    }

    public Service2 getService2() {
        return this.service2;
    }
}

/**
 * ׼service2
 */
public class Service2 {
    private Service1 service1;

    public void setService1(Service1 service1) {
        this.service1 = service1;
    }

    public Service1 getService1() {
        return this.service1;
    }
}

/**
 * е
 */
public class Main {
    public void main(String[] args) {
        // ׼
        Service1 service1 = new Service1();
        Service2 service2 = new Service2();
        // ໥
        service1.setService2(service2);
        service2.setService1(service1);
    }
}

ô spring У໥עԷʵʲôأ spring bean Ĵ̣ע⣺ǽ bean scope Ϊ singleton Ҳ scope Ϊ``

мҪ˵£

  1. ʵʹ jdk ṩķƴ java Ե 1 ᵽ Service1 ΪɼΪ Service1 service = new Service1()
  2. ע󣺻Ե 1 ᵽ Service1 ΪService1 ͨ @Autowired Զע Service2һǸ Service2 ֵḶ́ɼΪ service1.setService2(service2)
  3. singletonObjectsһ java ͱһ spring beanȻ󱣴浽 singletonObjects ˣǸ map``key bean ƣvalue beanֻ spring beanֻ java ʵ

ʵϣjava spring beanֻע룬гʼִ beanPorcessor ȣڱǷ spring bean ѭģصעѭصIJ衣

2.1 ѭ

˽ spring bean IJ֮󣬽Ǿѭ⣬ʽǰȷ

  • javaʵϣjava һж󶼿Գ֮Ϊ java Ϊ˵㣬ᵽ javaָʵɡδ spring bean ڶ
  • spring beanһ java 󣬲ҽ spring bean ڶ

spring bean Ĵ£

ͼ˵£

  1. service1 󴴽ɺspring service1 Ҫע service2Ȼȥ singletonObjects в service2ʱҲ service2ȻͿʼ service2 Ĵ̣
  2. service2 󴴽ɺspring service2 Ҫע service1Ȼȥ singletonObjects в service1ʱҲ service1Ϊһ service1 ûдɹ ȻͿʼ service1 Ĵ̣
  3. ص 1ٴοʼ service1 Ĵע̡

Ǿϲط֣ѭˣ

2.2 earlySingletonObjects ѭ

Ƿ£ѭֵԭڣ service2 ȡ service1 ʱ singletonObjects дʱ service1˻ service1 Ĵ̣´ service1ˣи󵨵뷨 service1 ʵͰ service1 ʱͷδע service1

ͼУ earlySingletonObjectsҲǸ mapͬ singletonObjects һkey bean ƣvalue һδעĶ

ͼ˵£

  1. service1 󴴽ɺȽ service1 earlySingletonObjectsȻע룻
  2. service1 עʱspring service1 Ҫע service2Ȼȥ earlySingletonObjects service2δҵȥ singletonObjects в service2δҵǾͿʼ service2 Ĵ̣
  3. service2 󴴽ɺȽ service2 earlySingletonObjectsȻע룻
  4. service2 עʱspring service2 Ҫע service1Ȼȥ earlySingletonObjects service1ҵˣͽ service1 ע뵽 service2 Уʱ service2 һ spring bean ˣ䱣浽 singletonObjects У
  5. 4 ǵõ service2Ȼע뵽 service1 Уʱ service1 Ҳһ spring bean䱣浽 singletonObjects С

ϲ裬Ƿ֣ѭõ˽

2.2 aop µѭ

ķǷֻҪһ earlySingletonObjects ѭܵõǣѭĵõ˽spring ioc ⣬һشܣaop aop ³ѭ

1. aop Ĵ

ʽ aop µѭǰȷ

  • ԭʼڴָδй aop Ķ󣬿 java Ҳδ aop spring bean
  • ``й aop Ķ󣬿 java й aop õĶ (й aopδע룬Ҳδгʼ)Ҳǽй aop spring bean.

aop δģ

2.1 е̣aop "ɴ" IJձ浽 singletonObjects еĶҲǴ

ԭʼ֮ʲôϵأôʾ££

public class ProxyObj extends Obj {

    // ԭʼ
    private Obj obj;

    ...
}

ʵϣ֮Ĺϵûô򵥣Ϊ˵⣬߹ϵ˼򻯣СֻҪףԭʼɡ

ԭʼαɴģԲο spring aop ֮ AnnotationAwareAspectJAutoProxyCreator £

ϴ̣ java ģ£

/**
 * ׼һ
 */
public class Obj1 {

}

/**
 * ׼һ࣬ڲһ Obj1
 */
public class Obj2 {

    private Obj1 obj1;

    // ʡ
    ...

}

/**
 * ׼Obj2Ĵ࣬ڲobj2Ķ
 */
public class ProxyObj2 extends Obj2 {

    private Obj2 obj2;

    public ProxyObj2(Obj2 obj2) {
        this.obj2 = obj2;
    }

    // ʡ
    ...

}

ţģ --> ע --> ɴ --> 浽С ˣ

public static main(String[] args) {
     // ׼һﱣڵĶ
     // 1\. ԪԭʼöѾע 
     // 2\. ԪǴöеԭжѾע 
     Collection<?> collection = new ArrayList();

     // ʼ Obj2 Ĵ
     // 1\.  Obj2 
     Obj2 obj2 = new Obj2();

     // 2\.  Obj2 ע obj1ʱûobj1Ҫobj1ٽע뵽Obj2
     Obj1 obj1 = new Obj1();
     obj2.setObj1(obj1);

     // 3\. Obj2Ĵ󣬴г Obj2ԭʼ
     ProxyObj2 proxyObj2 = new ProxyObj2(obj2);

     // 4\. proxyObj2Ѿڣ˽ӵʱ
     collection.add(proxyObj2); 

}

У

  • new Obj2() ģĴ
  • obj2.setObj1(xxx) ģע
  • new ProxyObj2(xxx) ģ
  • collection.add(xxx) ģӵеĹ

ģ£

  1. obj2
  2. Obj2 ע obj1ʱû obj1Ҫ obj1ٽע뵽 Obj2
  3. Obj2 Ĵ proxyObj2``proxyObj2 г Obj2 ԭʼ
  4. proxyObj2 Ѿڣ˽ӵʱ

ϸIJ裬ͻ֣ᷢĵ 2 3 ȫ˳Ҳû⣬ģ£

public static main(String[] args) {
     // ׼һﱣڵĶ
     // 1\. ԪԭʼöѾע 
     // 2\. ԪǴöеԭжѾע 
     Collection<?> collection = new ArrayList();

     // ʼ Obj2 Ĵ
     // 1\.  Obj2 
     Obj2 obj2 = new Obj2();

     // 2\. Obj2Ĵ󣬴г Obj2ԭʼ
     ProxyObj2 proxyObj2 = new ProxyObj2(obj2);

     // 3\.  obj2 ע obj1ʱûobj1Ҫobj1ٽע뵽Obj2
     Obj1 obj1 = new Obj1();
     // ע뵽ԭʼ
     obj2.setObj1(obj1);

     // 4\. proxyObj2Ѿڣ˽ӵʱ
     collection.add(proxyObj2); 

}

£

  1. obj2
  2. Obj2 Ĵ󣬴г Obj2 ԭʼ
  3. Obj2 ע obj1ʱû obj1Ҫ obj1ٽע뵽 Obj2
  4. proxyObj2 Ѿڣ˽ӵʱ

ӴϿproxyObj2() г ob2(ԭʼ)ɴ󣬼ԭʼע룬ȻӰմеԭʼҲע룬ͼʾ£

ٴ java spring bean IJкö࣬ǽעѭصIJ裬˽ spring bean ϸijʼ̣ɲ鿴 spring ̸֮

̽ڿ֣

  • --> ע --> ɴ --> 󱣴浽
  • (ԭʼ)--> ɴ (ǰ aop)--> ԭʼע --> 󱣴浽

ֶܴﵽĿģ浽еǴҴӦԭʼעμ̣Ǻ aop ѭĺģ˵ˣ**aop µѭ֮ܽΪǰ aop **

2. Ϊʲô earlySingletonObjects ޷ѭ

ǰҪ˵˴Ĵ̣ aop £ʹ earlySingletonObjects ѭʲô⣺

ͼ̣

  1. service1 󴴽ɺȽ service1 earlySingletonObjectsȻע룻
  2. service1 עʱspring service1 Ҫע service2Ȼȥ earlySingletonObjects service2δҵȥ singletonObjects в service2δҵǾͿʼ service2 Ĵ̣
  3. service2 󴴽ɺȽ service2 earlySingletonObjectsȻע룻
  4. service2 עʱspring service2 Ҫע service1Ȼȥ earlySingletonObjects service1ҵˣͽ service1 ע뵽 service2 УȻٽ aopʱ service2 һ󣬽䱣浽 singletonObjects У
  5. 4 ǵõ service2 ĴȻע뵽 service1 Уٶ service1 aopʱ service1 Ҳһ spring bean䱣浽 singletonObjects С

ʲôأϸ 4 ͻ֣ᷢע뵽 service2 service1 Ǵݹȫ֣յõ service1 service2 Ǵע뵽 service2 service1 ӦҲǴŶԡˣ aop £ѭֳˣ

2.3 spring Ľ

ǰᵽ aop £ earlySingletonObjects ܽѭ⣬ spring ôأspring ٴһ map ⣬Ҳdz˵ **spring ** map ˵£

  • һ singletonObjectsΪ ConcurrentHashMap<String, Object>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value spring beanע롢ʼ bean bean Ҫ aop洢ľǴ
  • earlySingletonObjectsΪ HashMap<String, Object>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value ʵɣδע bean bean Ҫ aop洢ľǴֻеԭʼδע룻
  • singletonFactoriesΪ HashMap<String, ObjectFactory>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value 洢һ lambda ʽ() -> getEarlyBeanReference(beanName, mbd, bean) getEarlyBeanReference е bean Ǹմɵ java beanûн spring ע룬Ҳû aop ( lambda ʽ)

Ϊ˵㣬 singletonObjects``earlySingletonObjects singletonFactories ֱΪ**һ**********

spring aop µѭ£

ͼűȽϸӣʵֿͱȽϼˣУ1~8 ǻȡ service1 ̣5.1~5.8 ǻȡ service2 ̣5.5.1 ٴλȡ service1 ֻ̣ڴ service1 ijʼУᴥ service2 ijʼ̣ service2 ijʼʱֻ service1˲ſһ𣬱Ƚϸӡ

ͼḶ́˵£飺̱Ƚϸӣȿ 1~8 IJٿ 5.1~5.8 IJࣩܶ

    1. service1ȡ service1һлȡʱǻȡģ
    1. service1 service1 ʵ
    1. service1ȡҪע뷽ԭʼϽлȡ
    1. service1֧ѭãͽ service1 ŵУǷ֧ѭǿõģ
    1. service1 service1 ע룬Ҫ service2ȻͿʼ service2 Ļȡ̣
  • 5.1 service2ȡ service2һлȡʱǻȡģ
  • 5.2 service2 service2 ʵ
  • 5.3 service2ȡҪע뷽ԭʼϽлȡ
  • 5.4 service2֧ѭãͽ service2 ŵУǷ֧ѭǿõģ
  • 5.5 service2 service2 ע룬Ҫ service1ȻͿʼ service1 Ļȡ̣
  • 5.5.1 service1: ȡ service1һлȡȡʱ service1 ڴУǼӶлȡմлȡˣ档ȡĹУ**ж service1 ǷҪ aopȻʼ aop **˷е service1 ǰ aop ǽѭĹؼ
  • 5.6 service2õ service1 service1 Ǵ󣩣ע뵽 service2 УŶ service2 aopõ service2 Ĵ
  • 5.7 service2֧ѭȴһٴλȡ service2δȡʹõǰ service2ǰ service2 Ǵ)
  • 5.8 service2 service2 ĴһУɾ棬ˣservice2 ʼɣע service1 Ǵһе service2 ҲǴ
    1. service1ص service1 ڣõ service2 service2 Ǵ󣩺󣬽ע뵽 service1``service1 עɣгʼж service1 ǷҪ aopȻ service1 Ҫ aop ģ 5.5.1 Ѿй aop ˣˣֱӷأһservice1 ԭʼ󣩣
    1. service1֧ѭȴһлȡ service1ȡٴӶлȡ service1Իȡ 5.5.1 ֪ service1 󣩣أ
    1. service1лȡĶעᵽһУɾ棬ˣservice1 ʼɣע service2 Ǵһе service1 ҲǴ

̣Ȼ϶࣬ service1 service2 ĻȡͬģֻҪŪ֮һĻȡ̣һ bean Ļȡ̾ͺͬˡ

УݽṹҪ˵£

  • singletonsCurrentlyInCreationΪ SetFromMap<String>λ DefaultSingletonBeanRegistryʽΪ Collections.newSetFromMap(new ConcurrentHashMap<>(16))Ǹ ConcurrentHashMap ʵֵ set洢ڴеĶ**жϵǰǷڴоͨҵǰǷ set **ģ
  • earlyProxyReferencesΪ ConcurrentHashMap<Object, Object>λ AbstractAutoProxyCreator洢ǰ aop Ķһǰ aopںٴ aop ʱͨж϶Ƿ earlyProxyReferences жȷҪҪ aopԴ֤ÿֻһ aop

ˣspring һṩ 5 ݽṹѭ⣬ܽ£

˵
singletonObjectsһΪ ConcurrentHashMap<String, Object>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value spring beanע롢ʼ bean bean Ҫ aop洢ľǴ
earlySingletonObjectsΪ HashMap<String, Object>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value ʵɣδע bean bean Ҫ aop洢ľǴֻеԭʼδע
singletonFactories****Ϊ HashMap<String, ObjectFactory>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value 洢һ lambda ʽ() -> getEarlyBeanReference(beanName, mbd, bean)``getEarlyBeanReference(xxx) е bean Ǹմɵ java beanûн spring ע룬Ҳû aop
singletonsCurrentlyInCreationΪ SetFromMap<String>λ DefaultSingletonBeanRegistryʽΪ Collections.newSetFromMap(new ConcurrentHashMap<>(16))Ǹ ConcurrentHashMap ʵֵ set洢ڴеĶ󣬿жϵǰǷڴ
earlyProxyReferencesΪ ConcurrentHashMap<Object, Object>λ AbstractAutoProxyCreator洢ǰ aop Ķ󣬿ж bean Ƿй aop֤ÿֻһ aop

Ͼ spring ѭˡ

3. ģ

ʽԴǰģѭḶ́£

/**
 * ׼һ࣬ڲһ Obj2
 */
public class Obj1 {
    // Ҫע obj2
    private Obj2 obj2;

    // ʡ
    ...
}

/**
 * ׼һ࣬ڲһ Obj1
 */
public class Obj2 {
    // Ҫע ob1
    private Obj1 obj1;

    // ʡ
    ...

}

/**
 * ׼Obj2Ĵ࣬ڲobj2Ķ
 */
public class ProxyObj2 extends Obj2 {
    // obj2ڲobj2ԭʼ
    private Obj2 obj2;

    public ProxyObj2(Obj2 obj2) {
        this.obj2 = obj2;
    }

    // ʡ
    ...

}

/**
 * ׼Obj1Ĵ࣬ڲobj1Ķ
 */
public class ProxyObj1 extends Obj1 {
    // obj2ڲobj1ԭʼ
    private Obj1 obj1;

    public ProxyObj1(Obj1 obj1) {
        this.obj1 = obj1;
    }

    // ʡ
    ...

}

  • ׼ࣺObj1 Obj2 Obj1 иΪ Obj2``Obj2 иΪ Obj1
  • ׼ Obj1 Obj2 Ĵ ProxyObj1``ProxyObj2 ProxyObj1``ProxyObj2 ֱһԣObj1 Obj2
  • new ObjX() ģĴ
  • objX.setObjX(xxx) ģע룻
  • new ProxyObjX(xxx) ģɣ
  • collection.add(xxx) ģӵеḶ́

ģյõĽΪ

  • շĶֱ proxyObj1``proxyObj2
  • ע뵽 obj1 е proxyObj2ע뵽 obj2 е proxyObj2

׼ѾˣǾͿʼģˡ

3.1 ģ 1

Ҫ

  • Obj1 Obj2 ϸ --> ע --> ɴ --> 浽С ̴
  • Ĵ̿Խ

Ŀ꣺

  • շĶֱ proxyObj1``proxyObj2
  • ע뵽 obj1 е proxyObj2ע뵽 obj2 е proxyObj2

£

public static main(String[] args) {
     // ׼һﱣڵĶ
     // 1\. ԪԭʼöѾע 
     // 2\. ԪǴöеԭжѾע 
     Collection<?> collection = new ArrayList();

     // 1\.  Obj1 
     Obj1 obj1 = new Obj1();

     // Ҫobj2Ĵע뵽obj1Уʱвûobj2Ĵлobj2Ĵ
     // һ.  Obj2 
     Obj2 obj2 = new Obj2();

     // obj2Ҫעobj1Ĵ󣬵ʱвûobj2ĴҪеobj1Ĵ

}

ִ ִ Obj2 ̾ͽвȥˣ

  • obj1 Ҫע obj2 Ĵ󣬵Ҳл obj2 Ĵ̣
  • obj2 Ҫע obj1 Ĵ󣬵Ҳл obj1 Ĵ̣
  • obj1 Ҫע obj2 Ĵ󣬵Ҳл obj2 Ĵ̣
  • ...

ѭ

ģδﵽԤĿ꣬ģʧܡ

3.1 ģ 2

Ҫ

  • Obj1 Obj2 ֮һ
    • --> ע --> ɴ --> 浽С ̴
    • (ԭʼ)--> ɴ --> ԭʼע --> 󱣴浽С ̴
  • Ĵ̿Խ

Ŀ꣺

  • շĶֱ proxyObj1``proxyObj2
  • ע뵽 obj1 е proxyObj2ע뵽 obj2 е proxyObj2### 1. ʲôѭ

spring עʱܻ໥ע

@Service
public class Service1 {
    @Autowired
    private Service2 service2;

}

@Service
public class Service2 {
    @Autowired
    private Service1 service1;

}

ϴ룬 Service1 ͨ @Autowird ע Service2 Service2 ͨ @Autowird ע Service1໥עͽѭ

2. ѭʲô

ʵϣ ABBҲAjava ȫֵ֧ģ

/**
 * ׼service1
 */
public class Service1 {
    private Service2 service2;

    public void setService2(Service2 service2) {
        this.service2 = service2;
    }

    public Service2 getService2() {
        return this.service2;
    }
}

/**
 * ׼service2
 */
public class Service2 {
    private Service1 service1;

    public void setService1(Service1 service1) {
        this.service1 = service1;
    }

    public Service1 getService1() {
        return this.service1;
    }
}

/**
 * е
 */
public class Main {
    public void main(String[] args) {
        // ׼
        Service1 service1 = new Service1();
        Service2 service2 = new Service2();
        // ໥
        service1.setService2(service2);
        service2.setService1(service1);
    }
}

ô spring У໥עԷʵʲôأ spring bean Ĵ̣ע⣺ǽ bean scope Ϊ singleton Ҳ scope Ϊ``

мҪ˵£

  1. ʵʹ jdk ṩķƴ java Ե 1 ᵽ Service1 ΪɼΪ Service1 service = new Service1()
  2. ע󣺻Ե 1 ᵽ Service1 ΪService1 ͨ @Autowired Զע Service2һǸ Service2 ֵḶ́ɼΪ service1.setService2(service2)
  3. singletonObjectsһ java ͱһ spring beanȻ󱣴浽 singletonObjects ˣǸ map``key bean ƣvalue beanֻ spring beanֻ java ʵ

ʵϣjava spring beanֻע룬гʼִ beanPorcessor ȣڱǷ spring bean ѭģصעѭصIJ衣

2.1 ѭ

˽ spring bean IJ֮󣬽Ǿѭ⣬ʽǰȷ

  • javaʵϣjava һж󶼿Գ֮Ϊ java Ϊ˵㣬ᵽ javaָʵɡδ spring bean ڶ
  • spring beanһ java 󣬲ҽ spring bean ڶ

spring bean Ĵ£

ͼ˵£

  1. service1 󴴽ɺspring service1 Ҫע service2Ȼȥ singletonObjects в service2ʱҲ service2ȻͿʼ service2 Ĵ̣
  2. service2 󴴽ɺspring service2 Ҫע service1Ȼȥ singletonObjects в service1ʱҲ service1Ϊһ service1 ûдɹ ȻͿʼ service1 Ĵ̣
  3. ص 1ٴοʼ service1 Ĵע̡

Ǿϲط֣ѭˣ

2.2 earlySingletonObjects ѭ

Ƿ£ѭֵԭڣ service2 ȡ service1 ʱ singletonObjects дʱ service1˻ service1 Ĵ̣´ service1ˣи󵨵뷨 service1 ʵͰ service1 ʱͷδע service1

ͼУ earlySingletonObjectsҲǸ mapͬ singletonObjects һkey bean ƣvalue һδעĶ

ͼ˵£

  1. service1 󴴽ɺȽ service1 earlySingletonObjectsȻע룻
  2. service1 עʱspring service1 Ҫע service2Ȼȥ earlySingletonObjects service2δҵȥ singletonObjects в service2δҵǾͿʼ service2 Ĵ̣
  3. service2 󴴽ɺȽ service2 earlySingletonObjectsȻע룻
  4. service2 עʱspring service2 Ҫע service1Ȼȥ earlySingletonObjects service1ҵˣͽ service1 ע뵽 service2 Уʱ service2 һ spring bean ˣ䱣浽 singletonObjects У
  5. 4 ǵõ service2Ȼע뵽 service1 Уʱ service1 Ҳһ spring bean䱣浽 singletonObjects С

ϲ裬Ƿ֣ѭõ˽

2.2 aop µѭ

ķǷֻҪһ earlySingletonObjects ѭܵõǣѭĵõ˽spring ioc ⣬һشܣaop aop ³ѭ

1. aop Ĵ

ʽ aop µѭǰȷ

  • ԭʼڴָδй aop Ķ󣬿 java Ҳδ aop spring bean
  • ``й aop Ķ󣬿 java й aop õĶ (й aopδע룬Ҳδгʼ)Ҳǽй aop spring bean.

aop δģ

2.1 е̣aop "ɴ" IJձ浽 singletonObjects еĶҲǴ

ԭʼ֮ʲôϵأôʾ££

public class ProxyObj extends Obj {

    // ԭʼ
    private Obj obj;

    ...
}

ʵϣ֮Ĺϵûô򵥣Ϊ˵⣬߹ϵ˼򻯣СֻҪףԭʼɡ

ԭʼαɴģԲο spring aop ֮ AnnotationAwareAspectJAutoProxyCreator £

ϴ̣ java ģ£

/**
 * ׼һ
 */
public class Obj1 {

}

/**
 * ׼һ࣬ڲһ Obj1
 */
public class Obj2 {

    private Obj1 obj1;

    // ʡ
    ...

}

/**
 * ׼Obj2Ĵ࣬ڲobj2Ķ
 */
public class ProxyObj2 extends Obj2 {

    private Obj2 obj2;

    public ProxyObj2(Obj2 obj2) {
        this.obj2 = obj2;
    }

    // ʡ
    ...

}

ţģ --> ע --> ɴ --> 浽С ˣ

public static main(String[] args) {
     // ׼һﱣڵĶ
     // 1\. ԪԭʼöѾע 
     // 2\. ԪǴöеԭжѾע 
     Collection<?> collection = new ArrayList();

     // ʼ Obj2 Ĵ
     // 1\.  Obj2 
     Obj2 obj2 = new Obj2();

     // 2\.  Obj2 ע obj1ʱûobj1Ҫobj1ٽע뵽Obj2
     Obj1 obj1 = new Obj1();
     obj2.setObj1(obj1);

     // 3\. Obj2Ĵ󣬴г Obj2ԭʼ
     ProxyObj2 proxyObj2 = new ProxyObj2(obj2);

     // 4\. proxyObj2Ѿڣ˽ӵʱ
     collection.add(proxyObj2); 

}

У

  • new Obj2() ģĴ
  • obj2.setObj1(xxx) ģע
  • new ProxyObj2(xxx) ģ
  • collection.add(xxx) ģӵеĹ

ģ£

  1. obj2
  2. Obj2 ע obj1ʱû obj1Ҫ obj1ٽע뵽 Obj2
  3. Obj2 Ĵ proxyObj2``proxyObj2 г Obj2 ԭʼ
  4. proxyObj2 Ѿڣ˽ӵʱ

ϸIJ裬ͻ֣ᷢĵ 2 3 ȫ˳Ҳû⣬ģ£

public static main(String[] args) {
     // ׼һﱣڵĶ
     // 1\. ԪԭʼöѾע 
     // 2\. ԪǴöеԭжѾע 
     Collection<?> collection = new ArrayList();

     // ʼ Obj2 Ĵ
     // 1\.  Obj2 
     Obj2 obj2 = new Obj2();

     // 2\. Obj2Ĵ󣬴г Obj2ԭʼ
     ProxyObj2 proxyObj2 = new ProxyObj2(obj2);

     // 3\.  obj2 ע obj1ʱûobj1Ҫobj1ٽע뵽Obj2
     Obj1 obj1 = new Obj1();
     // ע뵽ԭʼ
     obj2.setObj1(obj1);

     // 4\. proxyObj2Ѿڣ˽ӵʱ
     collection.add(proxyObj2); 

}

£

  1. obj2
  2. Obj2 Ĵ󣬴г Obj2 ԭʼ
  3. Obj2 ע obj1ʱû obj1Ҫ obj1ٽע뵽 Obj2
  4. proxyObj2 Ѿڣ˽ӵʱ

ӴϿproxyObj2() г ob2(ԭʼ)ɴ󣬼ԭʼע룬ȻӰմеԭʼҲע룬ͼʾ£

ٴ java spring bean IJкö࣬ǽעѭصIJ裬˽ spring bean ϸijʼ̣ɲ鿴 spring ̸֮

̽ڿ֣

  • --> ע --> ɴ --> 󱣴浽
  • (ԭʼ)--> ɴ (ǰ aop)--> ԭʼע --> 󱣴浽

ֶܴﵽĿģ浽еǴҴӦԭʼעμ̣Ǻ aop ѭĺģ˵ˣ**aop µѭ֮ܽΪǰ aop **

2. Ϊʲô earlySingletonObjects ޷ѭ

ǰҪ˵˴Ĵ̣ aop £ʹ earlySingletonObjects ѭʲô⣺

ͼ̣

  1. service1 󴴽ɺȽ service1 earlySingletonObjectsȻע룻
  2. service1 עʱspring service1 Ҫע service2Ȼȥ earlySingletonObjects service2δҵȥ singletonObjects в service2δҵǾͿʼ service2 Ĵ̣
  3. service2 󴴽ɺȽ service2 earlySingletonObjectsȻע룻
  4. service2 עʱspring service2 Ҫע service1Ȼȥ earlySingletonObjects service1ҵˣͽ service1 ע뵽 service2 УȻٽ aopʱ service2 һ󣬽䱣浽 singletonObjects У
  5. 4 ǵõ service2 ĴȻע뵽 service1 Уٶ service1 aopʱ service1 Ҳһ spring bean䱣浽 singletonObjects С

ʲôأϸ 4 ͻ֣ᷢע뵽 service2 service1 Ǵݹȫ֣յõ service1 service2 Ǵע뵽 service2 service1 ӦҲǴŶԡˣ aop £ѭֳˣ

2.3 spring Ľ

ǰᵽ aop £ earlySingletonObjects ܽѭ⣬ spring ôأspring ٴһ map ⣬Ҳdz˵ **spring ** map ˵£

  • һ singletonObjectsΪ ConcurrentHashMap<String, Object>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value spring beanע롢ʼ bean bean Ҫ aop洢ľǴ
  • earlySingletonObjectsΪ HashMap<String, Object>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value ʵɣδע bean bean Ҫ aop洢ľǴֻеԭʼδע룻
  • singletonFactoriesΪ HashMap<String, ObjectFactory>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value 洢һ lambda ʽ() -> getEarlyBeanReference(beanName, mbd, bean) getEarlyBeanReference е bean Ǹմɵ java beanûн spring ע룬Ҳû aop ( lambda ʽ)

Ϊ˵㣬 singletonObjects``earlySingletonObjects singletonFactories ֱΪ**һ**********

spring aop µѭ£

ͼűȽϸӣʵֿͱȽϼˣУ1~8 ǻȡ service1 ̣5.1~5.8 ǻȡ service2 ̣5.5.1 ٴλȡ service1 ֻ̣ڴ service1 ijʼУᴥ service2 ijʼ̣ service2 ijʼʱֻ service1˲ſһ𣬱Ƚϸӡ

ͼḶ́˵£飺̱Ƚϸӣȿ 1~8 IJٿ 5.1~5.8 IJࣩܶ

    1. service1ȡ service1һлȡʱǻȡģ
    1. service1 service1 ʵ
    1. service1ȡҪע뷽ԭʼϽлȡ
    1. service1֧ѭãͽ service1 ŵУǷ֧ѭǿõģ
    1. service1 service1 ע룬Ҫ service2ȻͿʼ service2 Ļȡ̣
  • 5.1 service2ȡ service2һлȡʱǻȡģ
  • 5.2 service2 service2 ʵ
  • 5.3 service2ȡҪע뷽ԭʼϽлȡ
  • 5.4 service2֧ѭãͽ service2 ŵУǷ֧ѭǿõģ
  • 5.5 service2 service2 ע룬Ҫ service1ȻͿʼ service1 Ļȡ̣
  • 5.5.1 service1: ȡ service1һлȡȡʱ service1 ڴУǼӶлȡմлȡˣ档ȡĹУ**ж service1 ǷҪ aopȻʼ aop **˷е service1 ǰ aop ǽѭĹؼ
  • 5.6 service2õ service1 service1 Ǵ󣩣ע뵽 service2 УŶ service2 aopõ service2 Ĵ
  • 5.7 service2֧ѭȴһٴλȡ service2δȡʹõǰ service2ǰ service2 Ǵ)
  • 5.8 service2 service2 ĴһУɾ棬ˣservice2 ʼɣע service1 Ǵһе service2 ҲǴ
    1. service1ص service1 ڣõ service2 service2 Ǵ󣩺󣬽ע뵽 service1``service1 עɣгʼж service1 ǷҪ aopȻ service1 Ҫ aop ģ 5.5.1 Ѿй aop ˣˣֱӷأһservice1 ԭʼ󣩣
    1. service1֧ѭȴһлȡ service1ȡٴӶлȡ service1Իȡ 5.5.1 ֪ service1 󣩣أ
    1. service1лȡĶעᵽһУɾ棬ˣservice1 ʼɣע service2 Ǵһе service1 ҲǴ

̣Ȼ϶࣬ service1 service2 ĻȡͬģֻҪŪ֮һĻȡ̣һ bean Ļȡ̾ͺͬˡ

УݽṹҪ˵£

  • singletonsCurrentlyInCreationΪ SetFromMap<String>λ DefaultSingletonBeanRegistryʽΪ Collections.newSetFromMap(new ConcurrentHashMap<>(16))Ǹ ConcurrentHashMap ʵֵ set洢ڴеĶ**жϵǰǷڴоͨҵǰǷ set **ģ
  • earlyProxyReferencesΪ ConcurrentHashMap<Object, Object>λ AbstractAutoProxyCreator洢ǰ aop Ķһǰ aopںٴ aop ʱͨж϶Ƿ earlyProxyReferences жȷҪҪ aopԴ֤ÿֻһ aop

ˣspring һṩ 5 ݽṹѭ⣬ܽ£

˵
singletonObjectsһΪ ConcurrentHashMap<String, Object>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value spring beanע롢ʼ bean bean Ҫ aop洢ľǴ
earlySingletonObjectsΪ HashMap<String, Object>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value ʵɣδע bean bean Ҫ aop洢ľǴֻеԭʼδע
singletonFactories****Ϊ HashMap<String, ObjectFactory>λ DefaultSingletonBeanRegistry Уkey Ϊ beanName``value 洢һ lambda ʽ() -> getEarlyBeanReference(beanName, mbd, bean)``getEarlyBeanReference(xxx) е bean Ǹմɵ java beanûн spring ע룬Ҳû aop
singletonsCurrentlyInCreationΪ SetFromMap<String>λ DefaultSingletonBeanRegistryʽΪ Collections.newSetFromMap(new ConcurrentHashMap<>(16))Ǹ ConcurrentHashMap ʵֵ set洢ڴеĶ󣬿жϵǰǷڴ
earlyProxyReferencesΪ ConcurrentHashMap<Object, Object>λ AbstractAutoProxyCreator洢ǰ aop Ķ󣬿ж bean Ƿй aop֤ÿֻһ aop

Ͼ spring ѭˡ

3. ģ

ʽԴǰģѭḶ́£

/**
 * ׼һ࣬ڲһ Obj2
 */
public class Obj1 {
    // Ҫע obj2
    private Obj2 obj2;

    // ʡ
    ...
}

/**
 * ׼һ࣬ڲһ Obj1
 */
public class Obj2 {
    // Ҫע ob1
    private Obj1 obj1;

    // ʡ
    ...

}

/**
 * ׼Obj2Ĵ࣬ڲobj2Ķ
 */
public class ProxyObj2 extends Obj2 {
    // obj2ڲobj2ԭʼ
    private Obj2 obj2;

    public ProxyObj2(Obj2 obj2) {
        this.obj2 = obj2;
    }

    // ʡ
    ...

}

/**
 * ׼Obj1Ĵ࣬ڲobj1Ķ
 */
public class ProxyObj1 extends Obj1 {
    // obj2ڲobj1ԭʼ
    private Obj1 obj1;

    public ProxyObj1(Obj1 obj1) {
        this.obj1 = obj1;
    }

    // ʡ
    ...

}

  • ׼ࣺObj1 Obj2 Obj1 иΪ Obj2``Obj2 иΪ Obj1
  • ׼ Obj1 Obj2 Ĵ ProxyObj1``ProxyObj2 ProxyObj1``ProxyObj2 ֱһԣObj1 Obj2
  • new ObjX() ģĴ
  • objX.setObjX(xxx) ģע룻
  • new ProxyObjX(xxx) ģɣ
  • collection.add(xxx) ģӵеḶ́

ģյõĽΪ

  • շĶֱ proxyObj1``proxyObj2
  • ע뵽 obj1 е proxyObj2ע뵽 obj2 е proxyObj2

׼ѾˣǾͿʼģˡ

3.1 ģ 1

Ҫ

  • Obj1 Obj2 ϸ --> ע --> ɴ --> 浽С ̴
  • Ĵ̿Խ

Ŀ꣺

  • շĶֱ proxyObj1``proxyObj2
  • ע뵽 obj1 е proxyObj2ע뵽 obj2 е proxyObj2

£

public static main(String[] args) {
     // ׼һﱣڵĶ
     // 1\. ԪԭʼöѾע 
     // 2\. ԪǴöеԭжѾע 
     Collection<?> collection = new ArrayList();

     // 1\.  Obj1 
     Obj1 obj1 = new Obj1();

     // Ҫobj2Ĵע뵽obj1Уʱвûobj2Ĵлobj2Ĵ
     // һ.  Obj2 
     Obj2 obj2 = new Obj2();

     // obj2Ҫעobj1Ĵ󣬵ʱвûobj2ĴҪеobj1Ĵ

}

ִ ִ Obj2 ̾ͽвȥˣ

  • obj1 Ҫע obj2 Ĵ󣬵Ҳл obj2 Ĵ̣
  • obj2 Ҫע obj1 Ĵ󣬵Ҳл obj1 Ĵ̣
  • obj1 Ҫע obj2 Ĵ󣬵Ҳл obj2 Ĵ̣
  • ...

ѭ

ģδﵽԤĿ꣬ģʧܡ

3.1 ģ 2

Ҫ

  • Obj1 Obj2 ֮һ
    • --> ע --> ɴ --> 浽С ̴
    • (ԭʼ)--> ɴ --> ԭʼע --> 󱣴浽С ̴
  • Ĵ̿Խ

Ŀ꣺

  • շĶֱ proxyObj1``proxyObj2
  • ע뵽 obj1 е proxyObj2ע뵽 obj2 е proxyObj2

ʾ£

 public static main(String[] args) {
      // ׼һﱣڵĶ
      // 1\. ԪԭʼöѾע 
      // 2\. ԪǴöеԭжѾע 
      Collection<?> collection = new ArrayList();

      // 1\.  Obj1 
      Obj1 obj1 = new Obj1();

      // Ҫobj2Ĵע뵽obj1Уʱвûobj2Ĵлobj2Ĵ
      // һ.  Obj2 
      Obj2 obj2 = new Obj2();

      // 2\.  Obj1 ǰ
      ProxyObj1 proxyObj1 = new ProxyObj1(obj1);

      // .  proxyObj1 ע뵽 obj2 
      obj2.setObj1(proxyObj1);

      // .  obj2Ĵ
      ProxyObj2 proxyObj2 = new ProxyObj2(obj2);

      // . proxyObj2 Ѿڣӵʱ
      collection.add(proxyObj2);

      // ʱѾ obj2 Ĵˣobj1
      // 3\.  proxyObj2 ע뵽 obj1 
      obj1.setObj2(proxyObj2);

      // 4\. proxyObj1 Ѿڣӵʱ
      collection.add(proxyObj1);
 }

ĴУobj1 1234 ʶobj2 һġ ʶ£

  • obj1 (ԭʼ)--> ɴ --> ԭʼע --> 󱣴浽С
  • obj2 --> ע --> ɴ --> 浽С

߶УﵽԤڵĿꡣ

3.3 ģеõĽ

Աģ룬ģ 2 ֮ ܴﵽԤĿ꣬ҪΪע obj2 obj1 ʱǰ obj1 Ĵ proxyObj1ʹ obj2 ̡ٴ֤ṩ aop ѭĽҪã

ƪľȵˣҪѭIJ spring ѭIJ裬ͨδģѭĽһƪǽ spring Դ spring νѭġ


ԭӣhttps://my.oschina.net/funcy/blog/4659555 ߸ˮƽд֮ӭָԭףҵתϵ߻Ȩҵתע

ʾ£

 public static main(String[] args) {
      // ׼һﱣڵĶ
      // 1\. ԪԭʼöѾע 
      // 2\. ԪǴöеԭжѾע 
      Collection<?> collection = new ArrayList();

      // 1\.  Obj1 
      Obj1 obj1 = new Obj1();

      // Ҫobj2Ĵע뵽obj1Уʱвûobj2Ĵлobj2Ĵ
      // һ.  Obj2 
      Obj2 obj2 = new Obj2();

      // 2\.  Obj1 ǰ
      ProxyObj1 proxyObj1 = new ProxyObj1(obj1);

      // .  proxyObj1 ע뵽 obj2 
      obj2.setObj1(proxyObj1);

      // .  obj2Ĵ
      ProxyObj2 proxyObj2 = new ProxyObj2(obj2);

      // . proxyObj2 Ѿڣӵʱ
      collection.add(proxyObj2);

      // ʱѾ obj2 Ĵˣobj1
      // 3\.  proxyObj2 ע뵽 obj1 
      obj1.setObj2(proxyObj2);

      // 4\. proxyObj1 Ѿڣӵʱ
      collection.add(proxyObj1);
 }

ĴУobj1 1234 ʶobj2 һġ ʶ£

  • obj1 (ԭʼ)--> ɴ --> ԭʼע --> 󱣴浽С
  • obj2 --> ע --> ɴ --> 浽С

߶УﵽԤڵĿꡣ

3.3 ģеõĽ

Աģ룬ģ 2 ֮ ܴﵽԤĿ꣬ҪΪע obj2 obj1 ʱǰ obj1 Ĵ proxyObj1ʹ obj2 ̡ٴ֤ṩ aop ѭĽҪã

ƪľȵˣҪѭIJ spring ѭIJ裬ͨδģѭĽһƪǽ spring Դ spring νѭġ


ԭӣhttps://my.oschina.net/funcy/blog/4659555 ߸ˮƽд֮ӭָԭףҵתϵ߻Ȩҵתע