JSF injected sessionscoped bean's method invocation become null pointer

731 views Asked by At

I use CDI to annotate beans. One bean called SessionManager holds the logined user information with the declaration:

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.ejb.Stateful;

@Stateful
@Named
@SessionScoped
public class SessionManagerImpl implements SessionManager, Serializable {
    ...
    public UserDto getLoginedUser() {
        ...
    }
}

And the other is called DashboardController as:

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.inject.Inject;

@Named
@RequestScoped
public class DashboardController implements Serializable {

    @Inject
    private SessionManager sessionManager;
    ...
    public void loadUserInfo() {
        ...
        UserDto userDto = sessionManager.getLoginedUser();
    }
}

The first time i open a page refer DashboardController, it works well. And if i continue to use the website, it still works. But if i don't click any page for some minutes, and come back to open the page, it will display a null pointer for the javassist$$getLoginedUser method invocation (sessionManager is not null when i use debug to watch). The session is still valid for i can get values from session map directly using faces context.

What's wrong with the SessionManager? Thanks.

1

There are 1 answers

0
John Ament On

This occurs because your Stateful Session Bean (EJB) has passivated, and is not reintroduced to your session. If there isn't a strong need to make your session scoped object a session bean, I would just make it a SessionScoped managed bean.