嗯,我正在尝试在 Quartz 作业中使用 RequestedScoped beans,请参阅:
public class JobRoboFtp implements Job {
@Inject
private AcervoVideoService acervoVideoService;
@Inject
private ConfiguracaoService configuracaoService;
@Inject
private FtpManager ftpManager;
但我总是:
No active contexts for scope type javax.enterprise.context.RequestScoped
好吧,如果我将此服务更改为 @Dependent 范围,则一切正常,但我想使用 RequestedScope。有什么办法吗?
最佳答案
由于您没有对版本说太多,我将假设 Weld 2.x(甚至 3.x)的一些最新版本 - 那么有一种方法。我还假设您正在谈论 SE 环境,否则请求范围将在请求期间自动激活。
如果我们谈论的是 CDI 1.2 (Weld 2.x),那么您需要添加对 Weld API 的显式依赖并使用它。依赖项是 org.jboss.weld:weld-api ,您正在寻找的功能是 @ActivateRequestContext
interceptor binding 。如果您正在寻找 Weld 文档的链接,其 here - 请注意,这是在 Weld 2.4 中添加的!
原理很简单 - 它在进入方法时拦截方法调用并激活上下文,然后在退出方法时销毁它。
@ActivateRequestContext
public void myMethod() {
// any content in here will have request context ACTIVE
doAwesomeThings();
}
如果我们谈论的是 CDI 2.0/Weld 3.x - 那么 CDI 采用了完全相同的方法(Weld 版本也适用)。您可以阅读here .
关于java - Quartz 作业中的 RequestedScope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46507227/