user1036998
user1036998

Reputation:

When injecting a @SessionScoped CDI bean in another one, does they belong to the same session?

I'm writing a JSF application, and I need to inject a named bean into another, for example:

@Named
@SessionScoped
public class BeanA implements Serializable{
    @Inject private BeanB b;
    public void doSth(){
        b.doSth();
    }
}

@Named
@SessionScoped
public class BeanB implements Serializable{}

Both of them are SessionScoped, and I hope an instance of BeanA and its injected BeanB would hold a same session state.

Does it pick or create an BeanB instance randomly or select the one with same session id?Thanks!

Upvotes: 3

Views: 1513

Answers (1)

covener
covener

Reputation: 17872

The operative part is that BeanB is session-scoped, so whenever you inject one (no matter how or where ('cept for @New)) it will be manged based on the current session.

Upvotes: 1

Related Questions