本文介绍了CDI渴望应用程序范围的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于不推荐使用 @ManagedBean(eager = true)
,您如何创建渴望应用程序范围的cdi bean ?
Since @ManagedBean(eager = true)
will be deprecated, How do you create a eager application scoped cdi bean?
推荐答案
通过使用CDI扩展框架解决了此问题:
Solved this by making use of CDI extension framework:
-
创建限定词:
Create Qualifier:
@Qualifier
@Target(value = {ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Eager {
}
使用观察者方法创建扩展实现:
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();
});
}
}
创建: META-INF / services / javax.enterprise.inject.spi.Extension
在上述文件中注册扩展实现类,即:
Register extension implementatoin class in above file i.e:
pypackage.EagerCDIExtension
这篇关于CDI渴望应用程序范围的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!