Eager ApplicationScoped zarządzał fasolami tworzonymi wielokrotnie
Mam kilkachętny ApplicationScoped
zarządzane fasole. Niektóre z nich są wstrzykiwane innym przezManagedProperty
adnotacja, tworząca drzewo zależności. Każdy zależny komponent bean manipuluje rodzicem po zakończeniu budowy.
Wydaje się jednak, że dla każdej iniekcji tworzona jest nowa instancja, co powoduje cofnięcie poprzednich manipulacji. Według mojego zrozumieniaApplicationScoped
Fasola powinna być tworzona tylko raz. Czy źle zrozumiałem lub dlaczego tak się dzieje? Czy to dlatego, że są chętni?
Oto przykład:
ParentBean.javapackage example;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
@ManagedBean(eager = true)
@ApplicationScoped
public class ParentBean
{
static int initCount = 0;
// ...
@PostConstruct
public void init()
{
++initCount; // Will end up being between 1 and 3. Expected always 1.
// ...
}
}
Child1Bean.javapackage example;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
@ManagedBean(eager = true)
@ApplicationScoped
public class Child1Bean
{
@ManagedProperty("#{parentBean}") ParentBean parentBean;
public ParentBean getParentBean()
{
return parentBean;
}
public void setParentBean(ParentBean parentBean)
{
this.parentBean = parentBean;
}
@PostConstruct
public void init()
{
// manipulate parentBean
}
}
Child2Bean.javapackage example;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
@ManagedBean(eager = true)
@ApplicationScoped
public class Child2Bean
{
@ManagedProperty("#{parentBean}") ParentBean parentBean;
public ParentBean getParentBean()
{
return parentBean;
}
public void setParentBean(ParentBean parentBean)
{
this.parentBean = parentBean;
}
@PostConstruct
public void init()
{
// manipulate parentBean
}
}