Which is equivalent to @ManagedBean (eager = true) in CDI

As we all know, it is recommended to use annotations from javax.enterprise.contextinstead javax.faces.bean, as they become obsolete.

And we all found that ManagedBeans c eager="true", annotated using @ApplicationScopedfrom javax.faces.beanand having a method @PostConstruct, is very useful for initializing web applications, for example: reading properties from the file system, initializing database connections, etc ...

Example:

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.annotation.PostConstruct;

@ApplicationScoped
@ManagedBean(eager=true)
public class someBean{

    @PostConstruct
    public void init(){
        //Do all needed application initialization.
    }
    ...
}

I want to know how I can get the same behavior if I use annotations from javax.enterprise.context.

Note: An @Startup annotation from javax.ejbwill help to run this code, but only at the time of webapp deployment, when the application server starts .

+4
3

CDI JSF. CDI ServletContextListener, webapp.

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Eager {
    //
}

@WebListener
public class EagerListener implements ServletContextListener{

    private static final AnnotationLiteral<Eager> EAGER_ANNOTATION = new AnnotationLiteral<Eager>() {
        private static final long serialVersionUID = 1L;
    };

    @Override
    public void contextInitialized(ServletContextEvent event) {
        CDI.current().select(EAGER_ANNOTATION).forEach(bean -> bean.toString());
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // NOOP.
    }

}

(: toString() )

import com.example.Eager;
import javax.enterprise.context.ApplicationScoped;

@Eager
@ApplicationScoped
public class YourEagerApplicationScopedBean {

    @PostConstruct
    public void init() {
        System.out.println("Application scoped init!");
    }
}

, JSF OmniFaces @Eager .

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

@Eager
@ApplicationScoped
public class YourEagerApplicationScopedBean {

    @PostConstruct
    public void init() {
        System.out.println("Application scoped init!");
    }
}

@SessionScoped, @ViewScoped @RequestScoped.

, , FacesContext bean. , CDI @Inject, ServletContext HttpSession.

+7

CDI 1.1 , :

public void processApplicationScopedInit(@Observes @Initialized(ApplicationScoped.class) ServletContext payload) {}
public void processApplicationScopedDestroyed(@Observes @Destroyed(ApplicationScoped.class) ServletContext payload) {}

: http://www.next-presso.com/2014/06/you-think-you-know-everything-about-cdi-events-think-again/

0

EJB CDI. @Singleton @Startup

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Singleton
@Startup
public class SomeBean {

    @PostConstruct
    public void init(){
        //Do all needed application initialization.
    }
    ...
}
0

Source: https://habr.com/ru/post/1648095/


All Articles