@ManagedBean(eager=true)
@ApplicationScoped
public class GlobalBean {
...
}
@ManagedBean annotation will be deprecated with JSF 2.2. It is highly recommended to use CDI (context dependency injection) beans in JEE environment. But what is the equivalent to the eager managed beans in CDI? Well, CDI is flexible, you can write portable CDI extensions. I asked Thomas Andraschko how to do this. Thomas is a CDI expert, co-owner of PrimeFaces Extensions and the committer in OpenWebBeans (OWB) project. His tip was to implement such extension as follows
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE})
public @interface Eager
{
}
package mydomain.mypackage;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessBean;
public class EagerExtension implements Extension {
private List<Bean<?>> eagerBeansList = new ArrayList<Bean<?>>();
public <T> void collect(@Observes ProcessBean<T> event) {
if (event.getAnnotated().isAnnotationPresent(Eager.class)
&& event.getAnnotated().isAnnotationPresent(ApplicationScoped.class)) {
eagerBeansList.add(event.getBean());
}
}
public void load(@Observes AfterDeploymentValidation event, BeanManager beanManager) {
for (Bean<?> bean : eagerBeansList) {
// note: toString() is important to instantiate the bean
beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean)).toString();
}
}
}
The extensions should be registered in a file META-INF/services/javax.enterprise.inject.spi.Extension. The file has only one line with a fully qualified path to the EagerExtension class, e.g. mydomain.mypackage.EagerExtension.Using is simple. Assume, we have an application scoped LayoutController CDI bean which is responsible for the entire layout configration. We can annotate it with @Eager and speed up the layout creation.
@ApplicationScoped
@Eager
@Named
public class LayoutController implements Serializable {
private LayoutOptions layoutOptions;
@PostConstruct
protected void initialize() {
layoutOptions = new LayoutOptions();
LayoutOptions panes = new LayoutOptions();
panes.addOption("slidable", false);
panes.addOption("spacing", 6);
layoutOptions.setPanesOptions(panes);
...
}
public LayoutOptions getLayoutOptions() {
return layoutOptions;
}
}
Have fun with CDI!
Hi Oleg,
ReplyDeletejust to understand: what's the advantage over a Singleton Session Bean with the @Startup annotation? Since you're using JEE anyway...
As front-end developer I don't use EJBs. This is another layer.
ReplyDeleteVery good just the thing I was looking for a couple of months ago.
ReplyDeleteI have been looking this type of information for a while because I am also working in Software Development Company Malaysia. I'd like to say thanks and want to say cheer!
ReplyDeleteYour post is very informative, I appreciate you for producing this great article, I want to say thanks to you for this awesome post..
ReplyDeleteOleg, in NetBeans 7.3, your code:
ReplyDelete@Target({TYPE})
causes this error:
The CDI Annotation is declared as Qualifier but it has wrong target values. Correct target values are '{METHOD, FIELD, PARAMETER, TYPE'} or '{FIELD, PARAMETER'}.
Do you have a fix or work-around?
Thanks, Bill
Just remove the @Qualifier from @Eager, this is not required.
ReplyDelete