[英]CDI Injection into a Servlet not working
我有一個與生產者的控制器。
@Named
@SessionScoped
public class UserController{
private User user;
//other methods
@Produces
@User
public User produceUser() {
return user;
}
}
如果將用戶注入到Servlet中,那么我將始終獲得同一用戶(我比較了對象ID)-也在不同的會話中。 如果我將用戶注入CDI Bean中,則它可以正常工作,並且可以獲取當前會話的用戶。
UserController
是基於會話作用域的,但生產者不是。 也就是說,生產者具有@Dependent
范圍,因此在初始化servlet時,將一次注入User
bean。
嘗試將@SessionScoped
添加到您的生產者方法中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.