原帖于 PrimeFaces 论坛@http: //forum.primefaces.org/viewtopic.php? f=3&t=29546
最近,我一直痴迷于评估我的应用程序的性能,调优 JPA 查询,用命名查询替换动态 SQL 查询,就在今天早上,我意识到 getter 方法在 Java Visual VM 中比其他方法更像是一个热点我的代码(或我的大部分代码)。
吸气剂方法:
PageNavigationController.getGmapsAutoComplete()
被 ui:include 引用在 index.xhtml 中
下面,您将看到 PageNavigationController.getGmapsAutoComplete() 是 Java Visual VM 中的一个热点(性能问题)。如果你往下看,在屏幕截图上,你会看到 getLazyModel(),PrimeFaces 惰性数据表 getter 方法,也是一个热点,只有当最终用户执行大量“惰性数据表”类型的东西/操作/任务时在应用程序中。:)

请参阅下面的(原始)代码。
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 赋值;请参阅下面的代码。
/*
* 2013-04-06 moved switch {...} to updateGmapsAutoComplete()
* because performance = 115ms (hot spot) while
* navigating through web app
*/
public Boolean getGmapsAutoComplete() {
return gmapsAutoComplete;
}
/*
* ALWAYS call this method after "page = ..."
*/
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”方法中添加代码。:)