【问题标题】:Share data between CDI decorators在 CDI 装饰器之间共享数据
【发布时间】:2020-04-07 14:06:34
【问题描述】:

是否有可能在两个 CDI 装饰器之间共享数据,这将一个接一个地调用?

在我的例子中,FirstDecorator 调用一个服务并取回一个 id。 SecondDecorator 需要知道这个 id 才能调用另一个服务。

【问题讨论】:

    标签: java decorator cdi


    【解决方案1】:

    Decorators 之间没有内置上下文或类似的内容。不必有。

    相反,你自己做这件事。每个Decorator 将@Inject 与您设计的对象相同,可能具有setId() 方法。那就是:

    // In decorator 1:
    @Inject
    private IdHolder idHolder;
    
    // In some method in decorator 1:
    this.idHolder.setId(theIdInQuestion);
    
    // In decorator 2:
    @Inject
    private IdHolder idHolder;
    
    // In some method in decorator 2:
    final Object id = this.idHolder.getId();
    
    // The IdHolder class:
    @ApplicationScoped // or some other scope that will "stick around" longer than @Dependent
    class IdHolder {
      private Object id;
      Object getId() { return this.id; }
      void setId(final Object id) { this.id = id; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2020-11-26
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      相关资源
      最近更新 更多