I want to inject the current user using @Inject @Current User
across all layers (i.e. web layer, EJB layer). In order to do this, I have the following CDI Producer method:
@Named
@SessionScoped
public class UserController {
@Resource SessionContext sessionContext;
@EJB UserDao userDao;
@Produces @Current
public User getCurrentUser() {
String username = sessionContext.getCallerPrincipal().getName();
User user = userDao.findByUsername(username);
}
}
@Qualifier
@Target({TYPE, METHOD, PARAMETER, FIELD})
@Retention(RUNTIME)
public @interface Current{}
Now, I want to inject the current user into an EJB stateless session bean as follows:
@Stateless
public class SomeBackendService {
@Inject @Current
private User user;
}
My question: Is the current user object always re-injected after the session changes, because the dependencies of a stateless session bean are normally injected once at creation time and the bean may be pooled and used across different sessions?