Is there any bean type or Java EE annotation to get a bean initialized at container startup without adding the bean to beans.xml?
Asked
Active
Viewed 2,584 times
2 Answers
4
Yes, there is.
This is what I use when I need a singleton, application scoped bean started on deploy:
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.annotation.PostConstruct;
@Named( "myBean" )
@ApplicationScoped
@Startup
@Singleton
public class MyBean {
@PostConstruct
public void postConstruct() {}
}
The postConstruct
method is added if you need any code to be executed besides initialization.

MiguelKVidal
- 1,498
- 1
- 15
- 23
2
You can use a Singleton
bean as shown in Initializing Singleton Session Beans:
import javax.ejb.Singleton;
import javax.ejb.Startup;
@Startup
@Singleton
public class StartupBean {
}

SilverNak
- 3,283
- 4
- 28
- 44