문제

I'm running GWT app on Jetty 6.1 with Weld 2.0. Got the next code:

    @SessionScoped
    public class SessionContext implements Serializable {

        @Inject
        private HttpSession httpSession;

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

What am I missing, why HttpSession is not injected? Reference says that Injecting the HttpSession will force the session to be created.

도움이 되었습니까?

해결책

Change the definition of

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

to

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

Also use constructor injection

Otherwise provide a setter method for httpSession

다른 팁

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!
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top