gpt4 book ai didi

jsf-2 - 如何更新 @Produces @ApplicationScoped 的集合?

转载 作者:行者123 更新时间:2023-12-04 19:32:15 31 4
gpt4 key购买 nike

我目前正在远离 Seam 的 @Factory注解。结合@Observer ,我可以这样做:

@Factory(value = "optionsList", scope = ScopeType.APPLICATION)
@Observer("entity.modified")
public List<MyBean> produceEntityOptions() {
List l = getEm().createQuery('select e from entity e').getResultList();
Contexts.getApplicationContext().set("optionsList", l);
return l;
}

这会缓存一个可能的选项列表,以供使用,例如<f:selectItems> (实际计算可能更复杂)。

我已经将它翻译成与 CDI 一起使用

@Produces @Named("optionsList") @ApplicationScoped
public List<MyBean> produceEntityOptions() {
return getEm().createQuery('select e from entity e').getResultList();
}

但是当外部事件表明缓存已经过时时,这将失去(仅)重新创建缓存的能力。我怎样才能取回它?

最佳答案

以下是您可以执行的操作:

@ApplicationScoped
public class MyListProducer {

// the current list
private List<MyBean> listOfBeans;

// resets / reloads/ refreshes list
private void loadList() {
this.listOfBeans = getEm().createQuery('select e from entity e').getResultList();
}

// initialize the list
@PostConstruct
protected void postConstruct() {
loadList();
}

// listen for the stale event - you'll have to create a type (maybe even qualifiers) yourself
private void resetList(@Observes MyCustomListIsStaleEvent evt) {
loadList();
}

// the producer - to ensure that the producer is called after you refresh the list, make the list of scope @Dependent instead of @ApplicationScoped
@Produces @Named("optionsList")
protected List<MyBean> getList() {
return this.listOfBeans;
}
}

我认为这实际上就是您想要的。但我不排除存在差异的可能性——我不太了解 Seam。

旁注:您应该考虑同步观察者和生产者方法,要么使用普通的旧同步,要么将上述内容设为有状态 session bean 并利用 EJB 同步机制。

关于jsf-2 - 如何更新 @Produces @ApplicationScoped 的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18774025/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com