Skip to content

循环依赖处理策略 #1

@Exodia

Description

@Exodia

构造函数注入时,有循环依赖

场景demo

ioc.create({
       A: {  
                creator: function(instanceB) { this.b = instanceB }
                args: [{  $ref :  'B' }]
       },

       B: {  
                creator: function(instanceA) { this.a = instanceA }
                args: [{  $ref :  'A' }]
       }
});

 ioc.getComponent('A', function(a){ //...  });

可能的策略

  1. 抛异常
  2. 将依赖设置为 null传入, 控制台丢警告

setter 注入时,有循环依赖

场景demo

ioc.create({
       A: {  
                creator: function() { // ... },
                properties: {  b: { $ref: 'B' } }
       },

       B: {  
                creator: function() { // ... },
                properties: {  a: { $ref: 'A' } }
       }
});

 ioc.getComponent('A', function(a){ //...  });

可能的策略

控制台发警告,同时创建好依赖实例后,调用对应 setter 传入实例即可,注入这块不会存在无限递归问题。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions