最初发布于PrimeFaces论坛@ http://forum.primefaces.org/viewtopic.php?f=3&t=29546
最近,我一直在痴迷于评估我的应用程序的性能,调整JPA查询,用命名查询替换动态SQL查询,而且就在今天早上,我意识到一个getter方法在Java Visual VM中比我的其他代码(或大部分代码)更成为热点。
Getter方法:
PageNavigationController.getGmapsAutoComplete()
在index.xhtml中,ui:include引用了以下内容:
如下所示,PageNavigationController.getGmapsAutoComplete()是Java Visual VM中的一个热点(性能问题)。如果您进一步查看屏幕截图,您会发现getLazyModel(),即PrimeFaces懒惰数据表格获取器方法,也是一个热点,仅当最终用户在应用程序中执行大量“懒惰数据表格”类型的操作/任务时。
请见下方(原始)代码。
public Boolean getGmapsAutoComplete() {
switch (page) {
case "/orders/pf_Add.xhtml":
case "/orders/pf_Edit.xhtml":
case "/orders/pf_EditDriverVehicles.xhtml":
gmapsAutoComplete = true;
break;
default:
gmapsAutoComplete = false;
break;
}
return gmapsAutoComplete;
}
在 index.xhtml 中被以下内容引用:
<h:head>
<ui:include src="#{pageNavigationController.gmapsAutoComplete ? '/head_gmapsAutoComplete.xhtml' : (pageNavigationController.gmaps ? '/head_gmaps.xhtml' : '/head_default.xhtml')}"/>
</h:head>
解决方案:由于这是一个“getter”方法,移动代码并在调用方法之前分配值给gmapsAutoComplete;请参见下面的代码。
public Boolean getGmapsAutoComplete() {
return gmapsAutoComplete;
}
private void updateGmapsAutoComplete() {
switch (page) {
case "/orders/pf_Add.xhtml":
case "/orders/pf_Edit.xhtml":
case "/orders/pf_EditDriverVehicles.xhtml":
gmapsAutoComplete = true;
break;
default:
gmapsAutoComplete = false;
break;
}
}
测试结果:PageNavigationController.getGmapsAutoComplete() 在 Java Visual VM 中不再是热点(甚至不再显示)
分享此主题,因为许多专家用户已建议初级 JSF 开发人员不要在“getter”方法中添加代码。:)
FacesContext#getCurrentPhaseId()
)在getter中有条件地处理任何业务逻辑。 - BalusC