2
Is there any way to start a CDI bean next to the application
I have the following bean:
@ApplicationScoped
@Named
public class MyBean implements Serializable {
@Inject
private Helper helper;
@PostConstruct
public void init() {
helper.doThings();
}
}
I would like it to be started together with the application, when it is deployed.
I’m using Weld-Servlet as the cdi implementation and tomcat7:
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>${weld-servlet.version}</version>
<scope>compile</scope>
</dependency>
@Startup? https://docs.oracle.com/javaee/6/api/javax/ejb/Startup.html
– Pedro
This Annotation is from EJB, I’m using only Tomcat 7 with cdi(Weld)
– Henrique Santiago
What version of CDI does the Weld container in question implement? From version 1.1 of CDI you can annotate a method to run during startup. Example:
public void init(@Observes @Initialized(ApplicationScoped.class) Object init)
. This seems to work with Tomcat 7: https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_Tomcat– Anthony Accioly