[英]CDI Injection into a Servlet not working
I have a Controller with a producer. 我有一个与生产者的控制器。
@Named
@SessionScoped
public class UserController{
private User user;
//other methods
@Produces
@User
public User produceUser() {
return user;
}
}
If I inject the user into a Servlet, I get always the same user(I compared the object IDs) - also in different sessions. 如果将用户注入到Servlet中,那么我将始终获得同一用户(我比较了对象ID)-也在不同的会话中。 If I inject the User in a CDI Bean, it's working and I get the user of the current session.
如果我将用户注入CDI Bean中,则它可以正常工作,并且可以获取当前会话的用户。
UserController
is session-scoped, but the producer is not. UserController
是基于会话作用域的,但生产者不是。 Ie the producer has @Dependent
scope, so the User
bean gets injected once when the servlet is initialized. 也就是说,生产者具有
@Dependent
范围,因此在初始化servlet时,将一次注入User
bean。
Try adding @SessionScoped
to your producer method. 尝试将
@SessionScoped
添加到您的生产者方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.