Minolan
Minolan

Reputation: 183

Weld injected HttpSession into the session scoped bean is null

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.

Upvotes: 1

Views: 1540

Answers (2)

Anton
Anton

Reputation: 1001

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!
}

Upvotes: 0

Apurv
Apurv

Reputation: 3753

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

Upvotes: 1

Related Questions