I have the following JSF 2.0 beans:
@ApplicationScoped
class GreedyBean {
Result doGreedyStuff(String userId) {
... // Takes lots of machine resources and time.
}
}
@SessionScoped
class MyPageBean {
String userId;
GreedyBean greedyBean;
String getFirstGreedyStuffStat() {
Result result = greedyBean.doGreedyStuff(userId);
return result.getString();
}
int getSecondGreedyStuffStat() {
Result result = greedyBean.doGreedyStuff(userId);
return result.getInt();
}
void setGreedyBean(GreedyBean greedyBean) { this.greedyBean = greedyBean; }
}
Then in my JSF page:
<h:outputText value="#{myPageBean.firstGreedyStuffStat}"/>
<h:outputText value="#{myPageBean.secondGreedyStuffStat}"/>
How do I refactor this to have only one call to GreedyBean::doGreedyStuff
without implementing a cache mechanism by myself? If JSF can't do that, saying so is a valid answer.
Notes:
- I have barely any knowledge in JSF: I'm only fixing this one bug and then I resume my usual other tasks.
- We use Spring for the injection, I tried to translate those in JSF annotation. If there's an error there, please indulge me.
- As @JasperdeVries mentioned, I can't put the call in the
@PostConstruct
ofMyPageBean
because the data wouldn't be up to date. The result should be up to date at the moment of the request, not at the start of the session. I understand the principle and will enforce it, though it doesn't help me in this situation.