The problem probably comes from the fact that your producer doesn't have an explicit scope, so it has the @Dependent
pseudo-scope. That means the producer code is called once at your EJB build-time and that the Injected value stays the same for all your EJB life.
You didn't mention how your @Stateful
EJB is used, but I assume that it's injected in another piece of code so your code keep it longer than a request.
To correct your bug your should give a normal scope to your producer (@RequestScoped
seems the best choice). So your producer code will become
@Produces @CurrentUser @RequestScoped
public User getUser() { ... }
Remember a producers doesn't inherit the scope from the bean that contains it.
With this normal scope, the user
bean won't be directly injected, but CDI will inject a proxy of it. Thus the currentUser
will stay the same only during the life of the current request.
Don't hesitate to read the dependent scope chapter of the CDI 1.1 spec, it's quite helpful