CDI Eager Application scoped bean

后端 未结 1 1902
夕颜
夕颜 2021-01-14 02:08

Since @ManagedBean(eager = true) will be deprecated, How do you create a eager application scoped cdi bean?

相关标签:
1条回答
  • 2021-01-14 02:50

    Solved this by making use of CDI extension framework:

    1. Create Qualifier:

      @Qualifier
      @Target(value = {ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER,     ElementType.TYPE})
      @Retention(value = RetentionPolicy.RUNTIME)
      public @interface Eager {
      }
      
    2. Create Extension implementation with observer method:

      public class EagerCDIExtension implements Extension {
      
              public void afterDeploymentValidation(@Observes AfterDeploymentValidation event, BeanManager beanManager) {
              beanManager.getBeans(Object.class, new AnnotationLiteral<Eager>() {
              }).parallelStream().filter(bean -> bean.getBeanClass().isAnnotationPresent(ApplicationScoped.class)).forEach(bean -> {
                  beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean)).toString();
              });
          }
      }
      
    3. Create: META-INF/services/javax.enterprise.inject.spi.Extension

    4. Register extension implementatoin class in above file i.e:

      pypackage.EagerCDIExtension
      
    0 讨论(0)
提交回复
热议问题