在CDI装饰器之间共享数据

问题描述 投票:0回答:1

是否有可能在两个将依次调用的CDI装饰器之间共享数据?

在我的情况下,FirstDecorator调用服务并获取ID。 SecondDecorator需要知道此ID才能调用另一个服务。

java decorator cdi
1个回答
0
投票

[没有内置上下文或Decorator之间共享的类似内容。没有必要。

相反,您可以自己做。每个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; }
}
© www.soinside.com 2019 - 2024. All rights reserved.