Quarkus(尚)不支持 EntityListener 中的注入。我需要一个解决方法来访问我当前的用户 bean

Injection in EntityListener is not (yet) supported in Quarkus. I need a workaround to acces my current User bean

This 错误阻止我将我的 CurrentUserBean 注入我的 EntityListener class。我需要一种在 CurrentUserBean 中访问我的 userId 的方法,以便实现我的 @PrePersist 和 @PreUpdate JPA 挂钩。

这是我通常会做的事情:

public class EntityListener {

@Inject
private CurrentUserBean currentUserBean;

@PrePersist
void onPersist(Object entity) {
    // [...]
    entity.setCreatedBy(currentUserBean.userId);
}

@PreUpdate
void onUpdate(Object entity) {
    // [...]
    entity.setUpdatedBy(currentUserBean.userId);
}

}

还有其他方法可以实现吗?我正在使用带有 JWT 令牌的无状态服务。

看来quarkus团队还没有实现这个功能

但是在github issue线程中有一个解决方法:

@ApplicationScoped
public class JPAEntityListenerInjectionWorkaround {
  @Inject FooBean fooBean;
}


FooBean fooBean = CDI.current().select(FooBean.class).get();

通过使用 CDI 依赖项查找功能,可以包含中间 bean 并在事件侦听器中 class 从上下文中检索此 bean 并访问信息。