@ApplicationScoped
public class ApplicationScopedClass {
//This class gets constructed once for the entire life of the application, and is shared among all CDI-managed classes throughout the life of the application.
@Inject
public ApplicationScopedClass(SomeDependency someDependency) {
doSomethingWith(someDependency);
}
public ApplicationScopedClass() {
//Note that it is required that an application scoped class have a public no-args constructor
}
}
Classes with @ApplicationScoped are created only once, and each object which depends on the class share the same instance. These classes are 'effectively' singletons, however it should be noted that there is nothing to prevent a coder from manually creating additional instances of the class. Thus, using @ApplicationScoped is useful for sharing context across the entire application, or as a performance optimization (if the class is expensive to construct instances of), but it should not be relied upon as an integrity measure for guaranteeing only one instance of a class exists.
Like request scoped beans, application scoped beans need to have a public no-args constructor.