@Singleton @Startup 依赖于@Stateless EJB
@Singleton @Startup depends on @Stateless EJB
我有以下设置:
@Singleton
@Startup
@DependsOn(value="DataSourceHandler")
public class TimerTask {
@EJB(name = "DataSourceHandler")
DataSourceHandler dataSourceHandler;
}
@Stateless(name = "DataSourceHandler")
public class DataSourceHandler {
... database operations
}
timertask 每 30 分钟运行一次,并在 DataSourceHandler EJB 的帮助下执行数据库操作。
这里的问题是我无法将 EJB 注入 Singleton
Timertask,因为单例只能依赖其他单例。然而 other questions 中提出的解决方案对我不起作用:
- 我无法将
DataSourceHandler
设为单例,因为它也用于应用程序的其他部分,而不是多线程保存。
- 我无法从
TimerTask
中删除 Singleton,因为 @Startup
注释需要它
如何将无状态注入单例?
您不需要此处需要 dependsOn 注释。
@dependson 用于以下情况:
Used to express an initialization dependency between singleton
components.
由于 DataSourceHandler 是一个 EJB,它将在您的单例注入此 EJB 时由容器实例化。
我有以下设置:
@Singleton
@Startup
@DependsOn(value="DataSourceHandler")
public class TimerTask {
@EJB(name = "DataSourceHandler")
DataSourceHandler dataSourceHandler;
}
@Stateless(name = "DataSourceHandler")
public class DataSourceHandler {
... database operations
}
timertask 每 30 分钟运行一次,并在 DataSourceHandler EJB 的帮助下执行数据库操作。
这里的问题是我无法将 EJB 注入 Singleton
Timertask,因为单例只能依赖其他单例。然而 other questions 中提出的解决方案对我不起作用:
- 我无法将
DataSourceHandler
设为单例,因为它也用于应用程序的其他部分,而不是多线程保存。 - 我无法从
TimerTask
中删除 Singleton,因为@Startup
注释需要它
如何将无状态注入单例?
您不需要此处需要 dependsOn 注释。 @dependson 用于以下情况:
Used to express an initialization dependency between singleton components.
由于 DataSourceHandler 是一个 EJB,它将在您的单例注入此 EJB 时由容器实例化。