现在在做一个项目,使用struts2+spring,在页面访问时感觉特别慢,就对action做了下测试,把执行时间打印出来,发现为action注入service(逻辑层类)时,两种不同的注入方法产生80甚至几百毫秒的速度差异,请看:
action类:
public class HubAction extends BaseActionSupport {
private HubService hubService;
// findHub
private long hubId;
private Hub hub;
/**
* 根据hubId查找hub
*
* @return
*/
public String findHub() {
long start = System.currentTimeMillis();
System.out.println("============action.findHub开始==>" + start);
hub = hubService.getHub(hubId);
long end = System.currentTimeMillis();
System.out.println("============action.findHub开始==>" + end);
System.out.println("============action.findHub差值==>" + (end - start));
return SUCCESS;
}
public void setHubService(HubService hubService) {
this.hubService = hubService;
}
public long getHubId() {
return hubId;
}
public void setHubId(long hubId) {
this.hubId = hubId;
}
public Hub getHub() {
return hub;
}
}
service类:
public class HubService extends BaseService {
private HubDao hubDao;
public Hub getHub(long hubId) {
long start = System.currentTimeMillis();
System.out.println("service.getHub开始==>" + start);
Hub hub = hubDao.getHub(hubId);
long end = System.currentTimeMillis();
System.out.println("service.getHub结束==>" + end);
System.out.println("service.getHub差值==>" + (end - start));
return hub;
}
public void setHubDao(HubDao hubDao) {
this.hubDao = hubDao;
}
}
第一种测试方法:
public class HubActionTest extends BaseActionTestCase {
private HubAction action;
private HubService hubService;
protected void setUp() throws Exception {
super.setUp();
hubService = (HubService) ctx.getBean("hubService");
}
protected void tearDown() throws Exception {
super.tearDown();
action = null;
}
public void testExecute() throws Exception {
action = new HubAction();
action.setHubService(hubService);
long hubId = 92387951116L;
action.setHubId(hubId);
for (int i = 0; i < 10; i++) {
System.out
.println("hubService.hashcode==>" + hubService.hashCode());
long s = System.currentTimeMillis();
System.out.println("------------------------testExecute开始==>" + s);
action.findHub();
long e = System.currentTimeMillis();
System.out.println("------------------------testExecute结束==>" + e);
System.out.println("------------------------差值==>" + (e - s));
}
// set mock response so setting cookies doesn't fail
ServletActionContext.setResponse(new MockHttpServletResponse());
}
}
[color=red]BaseActionTestCase来则appfuse项目,去掉了一些处理mail的代码。[/color]
public abstract class BaseActionTestCase extends TestCase {
protected transient final Log log = LogFactory.getLog(getClass());
protected static XmlWebApplicationContext ctx;
protected MockHttpServletRequest request = new MockHttpServletRequest();
// This static block ensures that Spring's BeanFactory is only loaded
// once for all tests
static {
String[] paths = { "classpath*:spring/applicationContext-basic.xml",
"classpath*:spring/applicationContext-database.xml",
"classpath*:spring/applicationContext-service-search.xml",
"classpath*:spring/applicationContext-service.xml" };
ctx = new XmlWebApplicationContext();
ctx.setConfigLocations(paths);
ctx.setServletContext(new MockServletContext(""));
ctx.refresh();
}
protected void setUp() throws Exception {
LocalizedTextUtil.addDefaultResourceBundle("ApplicationResources");
ActionContext.getContext().setSession(new HashMap());
ServletActionContext.setRequest(request);
}
protected void tearDown() throws Exception {
ActionContext.getContext().setSession(null);
}
}
第二种测试方法:
public class Hub3ActionTest extends TransactionalBaseTest {
private HubAction action;
private HubService hubService;
public void setHubService(HubService hubService) {
this.hubService = hubService;
}
public void testaaaa() {
action = new HubAction();
action.setHubService(hubService);
long hubId = 92387951116L;
action.setHubId(hubId);
for (int i = 0; i < 10; i++) {
System.out
.println("hubService.hashcode==>" + hubService.hashCode());
long s = System.currentTimeMillis();
System.out.println("------------------------testExecute开始==>" + s);
action.findHub();
long e = System.currentTimeMillis();
System.out.println("------------------------testExecute结束==>" + e);
System.out.println("------------------------差值==>" + (e - s));
}
}
}
public class TransactionalBaseTest extends AbstractTransactionalDataSourceSpringContextTests {
protected String[] getConfigLocations() {
setAutowireMode(AUTOWIRE_BY_NAME);
return new String[] { "classpath*:/**/applicationContext-*.xml" };
}
}
第一次测试打印:
hubService.hashcode==>14274282
------------------------testExecute开始==>1206848722765
============action.findHub开始==>1206848722765
service.getHub开始==>1206848723046
这里进入service用了281毫秒
service.getHub结束==>1206848723046
service.getHub差值==>0
============action.findHub开始==>1206848723203
============action.findHub差值==>438
------------------------testExecute结束==>1206848723203
------------------------差值==>438
总的执行时间 438 毫秒
.....打印了10次,以下省略
第二次测试打印:
hubService.hashcode==>9422759
------------------------testExecute开始==>1206843361421
============action.findHub开始==>1206843361421
service.getHub开始==>1206843361421
这里进入service用了0毫秒
service.getHub结束==>1206843361421
service.getHub差值==>0
============action.findHub开始==>1206843361421
============action.findHub差值==>0
------------------------testExecute结束==>1206843361421
------------------------差值==>0
总的执行时间 0 毫秒
.....打印了10次,以下省略
谁能帮忙解释下什么原因?