Java EE 6: controlling startup of managed beans with dependencies: CDI, EJB

后端 未结 1 1746
既然无缘
既然无缘 2021-01-19 03:00

I just read the very nice explanation of the various managed beans and their relations on Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.Ma

相关标签:
1条回答
  • 2021-01-19 03:21

    Excerpt from Java EE 6 Tutorial with some modifications:

    @Singleton
    @Startup
    public class BeanA { ... }
    
    @Qualifier
    @Target({FIELD, PARAMETER})
    @Retention(RUNTIME)
    public @interface EjbStarted {}
    
    @Singleton
    @Startup
    @DependsOn("BeanA", "BeanB", "BeanC")
    public class LastBean {
        @Inject @EjbStarted Event<String> event;
    
        @PostConstruct
        public void startService() {
            // At this moment PrimaryBean is ready for use
            event.fire("LastBean");
        }
    }
    
    public class CDIService {
        public void start(@Observes @EjbStarted String name) {
            if("LastBean".equals(name)) {
                startService();
            }
        }
    }
    

    UPDATE: While thinking about the problem, I somehow forgot that you want the initialization order in CDI beans, so the answer is somewhat out of context, sorry about that :)

    UPDATE 2: Added how to make a CDI service start after EJBs

    0 讨论(0)
提交回复
热议问题