Reputation: 18074
I have the following setup:
@Singleton
@Startup
@DependsOn(value="DataSourceHandler")
public class TimerTask {
@EJB(name = "DataSourceHandler")
DataSourceHandler dataSourceHandler;
}
@Stateless(name = "DataSourceHandler")
public class DataSourceHandler {
... database operations
}
The timertask runs once every 30 minutes and performs database operations with the help of the DataSourceHandler EJB.
The problem here is that I'm unable to inject the EJB into the Singleton
Timertask, because a singleton can only depend on other singletons. The solutions proposed in other questions don't work for me however:
DataSourceHandler
a Singleton because it is also used in other parts of the application and not multithreading-save.TimerTask
because it is required for the @Startup
annotationHow can I inject a stateless into a singleton?
Upvotes: 0
Views: 4438
Reputation: 1832
You do not need a dependsOn annoatation here. @dependson is used for the below case:
Used to express an initialization dependency between singleton components.
Since DataSourceHandler is an EJB, it will be instantiated by the container at the moment your singleton injects this EJB.
Upvotes: 2