CDI 扩展 - 如何处理限定符上的非绑定值

CDI Extension - How to handle nonbinding values on qualifier

我目前正在开发 CDI 扩展。我必须注册一些特殊的自定义 bean,这就是生产者不够用的原因。

目前这是在 AfterBeanDiscovery 阶段内完成的,方法是使用 abd.addBean() 方法注册 Bean<> 的自定义实现。

缺点是在 bean 的 create() 方法中我无法访问 InjectionPoint,它可能在注释中包含额外的配置。

作为一种可能性,我可以在 ProcessInjectionTarget 阶段收集我需要的所有 bean(因为我可以访问注释)并在 AfterBeanDiscovery 阶段注册它们中的每一个。 然后我必须使配置注释成为限定符和限定符绑定的属性(不使用 @Nonbinding)以防止不明确的依赖关系。

但随后我将注册大量配置不同的 bean。

是否有替代解决方案?是否可以访问 beans create() 方法中的 InjectionPoint

谢谢!

我想我找到了解决办法。

基本上我需要以某种方式访问​​ beans create() 方法中的当前 InjectionPoint,这样也可以访问注入点的注释。

为了获得注入点,下面的代码被截断了(灵感来自 OmniFaces, using Apache Deltaspike):

public class InjectionPointGenerator {

  @Inject
  private InjectionPoint injectionPoint;

  public InjectionPoint getInjectionPoint() {
    return injectionPoint;
  }
}


public class BeanUtils {

  protected BeanUtils() {
    // Prevent instantiation
  }

  public static InjectionPoint getInjectionPoint(@Nonnull CreationalContext context) {
    Bean<InjectionPointGenerator> bean = getBean(InjectionPointGenerator.class);
    BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
    InjectionPoint injectionPoint = bean.getInjectionPoints().iterator().next();
    return (InjectionPoint) beanManager.getInjectableReference(injectionPoint, context);
  }

  private static <T> Bean<T> getBean(@Nonnull Class<T> type) {
    return BeanProvider.getBeanDefinitions(type, false, true).iterator().next();
  }
}