有没有办法知道状态是否处于活动状态而不捕获ContextNotActiveException? [英] Is there a way to know if a state is active without catching ContextNotActiveException?
问题描述
我想知道 RequestScoped
上下文当前是否在方法中处于活动状态。
I would like to know if the RequestScoped
context is currently active in a method or not.
在此刻,这是我的工作:
At the moment, here is what I do :
@Inject private BeanManager beanManager;
public boolean isRequestScopeActive() {
try {
if (beanManager.getContext(RequestScoped.class).isActive()) {
return true;
} else {
return false;
}
} catch (final ContextNotActiveException e) {
return false;
}
}
我认为赶上 ContextNotActiveException
只是想知道作用域是否处于活动状态。
I think it's a bit heavy to catch a ContextNotActiveException
just to know if a scope is active or not.
您有更好的方法来了解状态(处于活动状态吗?是否在CDI中使用上下文?
Do you have any better way to know the state (active or not) of a context in CDI ?
推荐答案
不,在CDI 1.0中,我们唯一的选择是捕获ContextNotActiveException。
No, the only option we have in CDI 1.0 is to catch the ContextNotActiveException.
您的解决方案应该可以工作。
Your solution should work.
这实际上是一个问题,因为范围实现无法用于注入。
This actually ends up being a problem because scope implementation is not available for injection.
例如您不能只 @Inject RequestScopedContextImpl rq;
并检查 rq.isActive()
方法,因为我们没有不必知道CDI本身的实现细节就可以访问该类。
E.g. You can't just @Inject RequestScopedContextImpl rq;
and check the rq.isActive()
method, because we don't have access to that class without knowing the implementation details of CDI itself.
这篇关于有没有办法知道状态是否处于活动状态而不捕获ContextNotActiveException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!