Eager Initialization
Quite often, there are scenarios where it is good to have something ready to use without having to wait for intial setup. EJB Pooling ensures that Stateless and Message Driven beans can take advantage of eager initialzation. Singleton EJBs (introduced in EJB 3.1) were enriched with @Startup
and @DependsOn
annotations which has ensured that they don't lag behind their pooled counterparts. Some of the common use cases include bootstrapping global configurations, initializing expensive resources etc.
These annotations are meant for usage with Singleton beans only
@Startup
@Startup
This annotation marks a Singleton bean for initialization during application startup i.e. the container automatically creates an instance of the bean when the application initializes. This offers the advantage of being able to invoke business logic in an eager and automated fashion (without any manual intervention). One can conveniently leverage the @PostConstruct
call back method to execute the bootstrapping logic and also use @PreDestroy
to define destruction of expensive resources such as database connections
It's important to bear in mind that, in the absence of this annotation, a Singleton EJB is instantiated by the container when it is first used/invoked by the client
@DependsOn
@DependsOn
Just like @Startup
, the @DependsOn
annotation is tied to Singleton EJBs. As the name suggests, this annotation is used to establish dependencies between different Singleton beans.
One can specify multiple dependent Singleton beans as a part of this annotation. Declaring a dependency on one or multiple Singleton bean(s) ensures that the container
Initializes the dependent beans prior to conclusion of the setup process of the singleton (before invocation of its
@PostConstruct
method)Guarantees the presence of dependent bean instances before the destruction of the singleton bean instance (before completion of its
@PreDestroy
method)
Next up ...
Let's dive into some of the scheduler and timer related features which EJBs offer...
Last updated