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.
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 posts Startup POJO with WELD and Startup EJB doesn't work but neither helped
import
section of yourjava class
? – Aggressivejavax.ejb.Startup
andjavax.ejb.Singleton
and use only thejavax.inject.Singleton
. Please do not mixEJB
andCDI
. – Aggressivejavax.inject.Singleton
DOES NOT start when Tomcat starts. It only starts after the resource into which the Singleton is injected has been started. – Heathen