问题
I created a Java Web Application Project in NetBeans, and created a startup bean in it:
package malibu.util;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
@Stateless
@LocalBean
@javax.ejb.Startup
public class Startup {
@EJB
private ProviderEJB providerEJB;
@PostConstruct
public void onStartup() {
System.err.println("Initialization success.");
}
}
But the code is not called after I deploy the application. What can cause this?
回答1:
Try the following set of annotations:
@Singleton
@Startup
public class Startup {
@EJB
private ProviderEJB providerEJB;
@PostConstruct
public void onStartup() {
System.err.println("Initialization success.");
}
}
You will find more details here and in this book (chapter 2).
回答2:
The Startup annotation is for usage with Singleton beans, not with stateless beans. See the javadoc.
Also, @LocalBean is not needed in this case. This declares that you want an additional no-interface view, but this is only needed if the bean implements a remote or local business interface. If you omit it you get a no-interface view by default.
回答3:
http://docs.oracle.com/javaee/6/api/javax/ejb/Startup.html
Mark a singleton bean for eager initialization during the application startup sequence.
来源:https://stackoverflow.com/questions/6820838/startup-bean-not-called