Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDI Eager Application scoped bean

Tags:

cdi

jsf-2.2

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

like image 842
Sphynx Avatar asked Oct 01 '22 04:10

Sphynx


1 Answers

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
    
like image 93
Sphynx Avatar answered Oct 05 '22 12:10

Sphynx