本文介绍了Weld的创业课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用@Singleton和@Startup注释(在tomcat 7上运行)在Weld CDI中为我的应用程序定义一个启动类,但我的PostConstruct方法永远不会被调用。

I am trying to define a startup class for my application in Weld CDI with @Singleton and @Startup annotations (running on tomcat 7), but my PostConstruct method is never called.

这是我的Startup类:

Here is my Startup class:

import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.Startup;
import javax.inject.Inject;
import javax.ejb.Singleton;
import javax.persistence.EntityManager;

import se.raindance.squid.core.domain.SquidSettings;


@Singleton 
@Startup
public class InitSquid {

  @Inject   
  private Logger log;  

  @Inject
  EntityManager entityManager;

  @PostConstruct
  public void init() {

    System.out.println("startup!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! InitSquid");
    // Init Rainlets
InitRainlets initRainlets = new InitRainlets(entityManager);
    initRainlets.init();

    initSquidSettings();    
  }

  private void initSquidSettings() {
    List<SquidSettings> settingsList = (List<SquidSettings>) entityManager
        .createQuery(
            "select squidsettings from SquidSettings squidsettings")
        .getResultList();

    if (settingsList.size() == 0) {
        log.info("No SquidSettings entity exists in system, creating one");

        SquidSettings settings = new SquidSettings();
        settings.setSubledgerRestResourceURI("http://localhost:8080/subledger-webapp/resteasy/");
        entityManager.persist(settings);
    }
  }

}

我试过我在这两个帖子中找到的提示
和但没有帮助

I tried the hints which I found in these two postsStartup POJO with WELD and Startup EJB doesn't work but neither helped

推荐答案

您不能在CDI bean上使用@Startup或@ javax.ejb.Singleton注释:这些注释适用于EJB。并且EJB不适用于(普通)Tomcat。如果要在Tomcat上运行EE应用程序,请查看项目。

You can not use the @Startup or @javax.ejb.Singleton annotations on a CDI bean: those annotations are meant for EJB's. And EJB's won't work on (plain) Tomcat. If you want to run an EE application on Tomcat, take a look at the TomEE project.

但是,有一个事件。您可以编写一个观察者方法来处理该事件,如此博客文章所示:

However, there's an AfterDeploymentValidation event that's thrown after everything has been deployed. You can write an observer method to act on that event, as shown in this blog post here:



  • Eager CDI beans

这篇关于Weld的创业课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 02:22