2

I am looking for a solution to replace the @ManagedBean(eager=true) annotation in JSF 2.2.

I took good note of proposals such as which allow the execution of code at application start up.

I need however to access the FacesContext, so this solution does not work : FacesContext.getCurrentInstance() is still null at this time.

I tried to find alternatives to AfterDeploymentValidation in SPI, but found nothing.

As developpers of DeltaSpike do not seem to plan to implement a substitution to @ManagedBean(eager=true) and as OmiFaces does not yet support this kind of solution, see. I am sending a message in a bottle.

For the time being, I just access the @ManagedScoped(eager=true) bean, transformed into a @Named, in my webapp common xhtml header, but it is kind of ugly.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ludovic Pénet
  • 1,136
  • 1
  • 16
  • 32

2 Answers2

4

The best solution for a JSF application with DeltaSpike is the following:

@ApplicationScoped
public class MyBean
{
    public void init(@Observes PostConstructApplicationEvent event)
    {
        // init here
    }
}
tandraschko
  • 2,291
  • 13
  • 13
  • Hi Thomas. Thank you for your proposal, but it does not work. The method is never called, with both DeltaSpike versions 0.6 and 0.7-SNAPSHOT – Ludovic Pénet Apr 22 '14 at 12:00
  • Well, in fact, it works. Part of my problem was that the wrong servlet version was used in web.xml. Thx again ! – Ludovic Pénet Apr 22 '14 at 12:48
  • I found another use/case where it is not enough. When using @Observes PostConstructApplicationEvent, the init call is not enclosed in the various filtered configured in web.xml. So, in some cases, all the inits I need are not performed... – Ludovic Pénet Apr 22 '14 at 12:57
3

Since OmniFaces 1.8, there's a CDI compatible @Eager which not only works on @Named @ApplicationScoped, but als on CDI's @SessionScoped and @RequestScoped and OmniFaces @ViewScoped. See also the blog entry and the showcase example.

You can use it either with @Eager @ApplicationScoped:

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import org.omnifaces.cdi.Eager;

@Named
@Eager
@ApplicationScoped
public class Bean {
    // ...
}

or with @Startup, which is a stereotype for @Eager @ApplicationScoped:

import javax.inject.Named;
import org.omnifaces.cdi.Startup;

@Named
@Startup
public class Bean {
    // ...
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555