Search code examples
javajboss7.xresteasyejb-3.1session-scope

EJB 3.1 - Inject Session Scope Pojo


Using: EJB 3.1, JBoss AS 7, RestEasy.

I have a session scoped bean which I want to user to store user informations for the session.

import java.io.Serializable;
import javax.enterprise.context.SessionScoped
@SessionScoped
public class LoggedInUser implements Serializable {
    private String id;
    ...
}

If the user open my web application a filter extract header informations (application runs behind a webseal) which contains a user identification. I need to create a logged in user object (see LoggedInUser above) there (after calling ldap). Afterwards I want to inject this LoggedInUser object in different @Stateless Beans, but LoggedInUser is always "empty" (members are null).

Inject sample:

@Path("/country")
@Stateless
public class CountryController extends AbstractController {
@Inject
private Logger LOGGER;
@Inject
private LoggedInUser loggedInUser;
//@Inject dont work too..
//private Instance<LoggedInUser> loggedInstance

What do Im wrong?


Solution

  • Judging by the comment and the question - you need some help with EJB concepts.

    @SessionScoped beans are beans that have one instance per session. Every @Inject with bean like that during one session will reference the same object. That's it however - if you want your bean to contain some information specific to that session, you will need to put it there yourself, using setters or other methods like you would do in every normal Java class.