尽管有 @ApplicationScoped 注解,@Injected @ManagedBean 仍被重新初始化

@Injected @ManagedBean gets reinitialized despite of @ApplicationScoped annotation

我正在编写一个简单的 JSF 应用程序来转换货币。

有一个 Rates class 用于保存数据,Currencies class 用于管理请求,Manage class 用于添加新的货币。我的问题是:我希望货币以 属性 的汇率 class 的形式保留,因此我使用 @ApplicationScoped。但是我可以看到 Rates 每次请求都会重新初始化 。这是我的 Rates:

代码
@ManagedBean
@ApplicationScoped
public class Rates {

private Map<String, Double> rates = new HashMap<String, Double>();
private Set<String> currencies = new HashSet<String>(){
    {
        System.out.println("HashSet initializing");
    }
};
private List<String> list = new ArrayList<String>(Arrays.asList("Filip", "Kasia"));

public List<String> getList() {
    return list;
}

public void setList(List<String> list) {
    this.list = list;
}

public Rates() {
    rates.put("PLN_EUR", 0.25);
    rates.put("EUR_PLN", 4.0);
    currencies.add("PLN");
    currencies.add("EUR");
}

public Map<String, Double> getRates() {
    return rates;
}

public void setRates(Map<String, Double> rates) {
    this.rates = rates;
}

public Set<String> getCurrencies() {
    return currencies;
}

public void setCurrencies(Set<String> currencies) {
    this.currencies = currencies;
}

public void addRate(String firstCurrency, String secondCurrency){
    if(rates.containsKey(firstCurrency + "_" + secondCurrency))
        return;
    double rate = (double)(Math.random()*10);
    rates.put(firstCurrency + "_" + secondCurrency, rate);
    rates.put(secondCurrency + "_" + firstCurrency, 1 / rate);
    currencies.add(firstCurrency);
    currencies.add(secondCurrency);
    System.out.println(currencies);

}

public double getRate(String currencies){
    return rates.get(currencies);
}
}

下面是它的注入方式:

@ManagedBean
public class Manage {
private String firstCurrency;
private String secondCurrency;
private @Inject Rates rates;

public String getSecondCurrency() {
    return secondCurrency;
}

public void setSecondCurrency(String secondCurrency) {
    this.secondCurrency = secondCurrency;
}

public String getFirstCurrency() {
    return firstCurrency;
}

public void setFirstCurrency(String firstCurrency) {
    this.firstCurrency = firstCurrency;
}

public void addCurrencies(){
    rates.addRate(firstCurrency, secondCurrency);
}


}

如您所见,我为 属性 的 Rates 添加了实例初始化程序,设置 currencies 并且每个请求都会打印出文本 "HashSet initializing"。

@Inject 仅注入 CDI 管理的 bean。

@ManagedBean 声明 JSF 托管 bean 而不是 CDI 托管 bean。

Rates class 设为 CDI 托管 bean。

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

@Named
@ApplicationScoped
public class Rates {}

应该注意的是,一般建议是停止使用 JSF 托管 bean 工具并专门使用 CDI 托管 bean 工具。因此,如果您将所有其他 JSF 托管 bean 也设为 CDI 托管 bean 会更好。这就是为什么我不建议用它的 JSF 等价物 @ManagedProperty 替换 @Inject(这也可以工作)。

至于托管 bean 初始化,您不应在 class/instance 级别进行,而应在 @PostConstruct 注释方法中执行任务。

private Map<String, Double> rates;
private Set<String> currencies;
private List<String> list;

@PostConstruct
public void init() {
    rates = new HashMap<>();
    currencies = new HashSet<>();
    list = new ArrayList<>(Arrays.asList("Filip", "Kasia"));
}

这在 CDI 中尤为重要,因为它基于 class 创建和注入 proxies,因此 class 可能会在您意想不到的时候实例化。

另请参阅:

  • Backing beans (@ManagedBean) or CDI Beans (@Named)?