maja
maja

Reputation: 18074

@Singleton @Startup depends on @Stateless EJB

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:

How can I inject a stateless into a singleton?

Upvotes: 0

Views: 4438

Answers (1)

NiranjanBhat
NiranjanBhat

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

Related Questions