什麼是應用程序作用域Bean?我理解它是一種將在應用程序的生命中存在的bean,但這似乎並不是正確的解釋。我有一個應用程序,它在啓動時創建一個應用程序範圍的bean(eager = true),然後是一個試圖訪問應用程序範圍的bean的對象(也是應用程序範圍)的會話bean。但是當我嘗試從會話作用域bean訪問應用程序範圍的bean時,我得到一個空指針異常。下面是我的代碼摘錄:訪問應用程序作用域bean導致NullPointerException
應用範圍的bean:
@ManagedBean(eager=true)
@ApplicationScoped
public class Bean1 implements Serializable{
private static final long serialVersionUID = 12345L;
protected ArrayList<App> apps;
// construct apps so they are available for the session scoped bean
// do time consuming stuff...
// getters + setters
會話範圍的bean:
@ManagedBean
@SessionScoped
public class Bean2 implements Serializable{
private static final long serialVersionUID = 123L;
@Inject
private Bean1 bean1;
private ArrayList<App> apps = bean1.getApps(); // null pointer exception
這似乎是發生的是,Bean1創建,確實它的東西,然後被銷燬在Bean2可以訪問它之前。我希望使用應用程序作用域將保持Bean1的狀態,直到容器被關閉,或者應用程序被終止,但這似乎不是這種情況。