如何延迟加载@Named Bean

How to Lazy Load the @Named Bean

我有一个 SessionScoped Named Bean 如下:

@Named("userBean")
@SessionScoped
public class UserBean implements Serializable {
  .....
   @PostConstruct
      public void init()
       {
            ...call some methods to fetch some data
       }

现在的问题是,在部署应用程序时,将调用 init(),并且在整个会话期间不会再次调用它。 我的要求是仅在需要初始化 bean 时才调用 init()。 如何实现?

@Named("userBean")
@SessionScoped
@Lazy
public class UserBean implements Serializable

无论你有什么@Autowired UserBean

成功

@Lazy
@Autowired
UserBean userbean;