1

我正在使用 Weld 2.0 在 Jetty 6.1 上运行 GWT 应用程序。得到下一个代码:

    @SessionScoped
    public class SessionContext implements Serializable {

        @Inject
        private HttpSession httpSession;

        public SessionContext() {
            super();
            //at this point httpSession is null
        }
    }

我错过了什么,为什么没有注入 HttpSession?参考说Injecting the HttpSession will force the session to be created.

4

2 回答 2

1

改变定义

public SessionContext() {
        super();
        //at this point httpSession is null
}

public SessionContext(HttpSession httpSession) {
        super();
        this.httpSession = httpSession;
        //check session here
}

也使用构造函数注入

否则为httpSession

于 2013-03-27T11:38:10.020 回答
0

It's better to use @PostConstruct to annotate some other method, here you have :

Initializing a managed bean specifies the lifecycle callback method that the CDI framework should call after dependency injection but before the class is put into service.

This is exactly the place where your injections are done but no code has been invoked.

like this :

@PostConstruct
public void doMyStuff() {
  //feel free to access your injections here they are not null!
}
于 2014-08-26T11:00:58.370 回答