SessionScoped 托管 bean 注入不起作用

SessionScoped Managed bean injection is not working

这是我的 SessionScoped 托管 bean:

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

@Named("clientSessionBean")
@SessionScoped
public class ClientSessionManagedBean implements Serializable {
...
}

这是我的请求范围托管 bean

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

@Named("myBean")
@RequestScoped
public class MyManagedBean {

 @Inject
 private ClientSessionManagedBean clientSessionBean;
 ..
 }

价值 clientSessionBean 给我 null .

如何在请求范围的托管 bean 中注入 sessionScoped bean?

包裹有问题吗?

注入的资源仅在 构造函数具有 运行 之后可用,即在 @PostConstruct 期间及以后。来自 JSR-250 的规范文档:

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization

您应该能够从上面的摘录中推断出,bean 生命周期中的事件顺序是:

  1. 初始化即调用构造函数(实际机制比较复杂,归结起来就是这样)

  2. 执行注射

  3. 调用生命周期回调,即@PostConstruct。在这一点上,您可以使用在 #2

  4. 中创建的任何内容

相关

  • Why use @PostConstruct?
  • Initialization of List in a JSF Managed bean(第 3 点)