【发布时间】:2017-02-10 16:18:06
【问题描述】:
是否可以将@SessionScoped bean 用作自定义范围的上下文中的字段?
我正在使用 CDI 编写自定义范围(“ScreenScoped”),因此它的行为与 CDI 的 @ViewScoped 大致相同(我这样做是因为后者不兼容 WebSphere)。到目前为止,我的作用域就像@ApplicationScoped 那样。我想使用我的 @SessionScoped NavigationHandler 类(每次用户单击链接或按钮时调用)来确定我的 ScreenScoped 生命周期何时结束。但是,当我尝试使用 @Injected 字段时,我确实遇到了错误。
public class ScreenContext
implements Context
{
@Inject
private NavigationHandler navigationHandler;
...
}
因为这个@Inject而出现NullPointerException:
16:55:07,492 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (http-localhost/127.0.0.1:8443-10) Error Rendering View[/page/hello.xhtml]: javax.el.ELException: /page/hello.xhtml @24,58 visible="#{helloController.popupshowed}": java.lang.NullPointerException
...
Caused by: java.lang.NullPointerException
at com.mypackage.scope.screenscope.ScreenContext.get(ScreenContext.java:38) [myproject.jar:]
第 38 行是我第一次调用注入的字段:
System.out.println("Navigation is used: " + navigationHandler.getUserId());
【问题讨论】:
-
使用BeanManager获取NavigationHandler bean
-
由于自定义上下文是通过扩展注册的,因此无法执行注入
标签: java scope cdi weld custom-scope