是否有可能在两个 CDI 装饰器之间共享数据,这将依次调用?
在我的例子中,FirstDecorator 调用一个服务并返回一个 id。 SecondDecorator 需要知道这个 id 才能调用另一个服务。
最佳答案
装饰器之间没有内置的上下文或类似的共享内容。不一定有。
相反,您可以自己执行此操作。每个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; }
}
关于java - 在 CDI 装饰器之间共享数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59334115/