Java 将注入的HttpSession焊接到会话作用域bean中为null
我正在用Weld 2.0在Jetty 6.1上运行GWT应用程序。 获得下一个代码:Java 将注入的HttpSession焊接到会话作用域bean中为null,java,gwt,jetty,cdi,weld,Java,Gwt,Jetty,Cdi,Weld,我正在用Weld 2.0在Jetty 6.1上运行GWT应用程序。 获得下一个代码: @SessionScoped public class SessionContext implements Serializable { @Inject private HttpSession httpSession; public SessionContext() { super(); //at
@SessionScoped
public class SessionContext implements Serializable {
@Inject
private HttpSession httpSession;
public SessionContext() {
super();
//at this point httpSession is null
}
}
我遗漏了什么,为什么没有注入HttpSession?参考说明注入HttpSession将强制创建会话。更改
public SessionContext() {
super();
//at this point httpSession is null
}
到
也可以使用构造函数注入
否则,为
httpSession
提供setter方法来更改
public SessionContext() {
super();
//at this point httpSession is null
}
到
也可以使用构造函数注入
否则,请为
httpSession
提供setter方法。最好使用@PostConstruct
注释其他方法,您可以:
初始化托管bean指定生命周期回调方法
CDI框架应该在依赖项注入之后调用,但是
在课程投入使用之前
这正是您完成注入的地方,但没有调用任何代码
像这样:
@PostConstruct
public void doMyStuff() {
//feel free to access your injections here they are not null!
}
最好使用
@PostConstruct
注释其他方法,您有:
初始化托管bean指定生命周期回调方法
CDI框架应该在依赖项注入之后调用,但是
在课程投入使用之前
这正是您完成注入的地方,但没有调用任何代码
像这样:
@PostConstruct
public void doMyStuff() {
//feel free to access your injections here they are not null!
}
谢谢,这很有帮助。但我必须提到,无论如何,必须有一个无参数的默认构造函数。有一个默认的构造函数,并使用
@PostConstruct
方法。谢谢,这很有帮助。但我必须提到,无论如何,必须有一个无参数的默认构造函数。有一个默认的构造函数,并使用@PostConstruct
方法。