我用下面的方法查看了使用aspectj的spring框架的执行时间。只需要下载aspectjrt.jar并在eclipse中配置并转换为aspectj项目,并且需要spring的源文件。
package org.springframework;
import java.util.ArrayList;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AspectJAnnotation {
protected long starttime;
protected long endtime;
protected ExecutionVaribale methodlevleinformation=new ExecutionVaribale();
protected List<ExecutionVaribale> liststore = new ArrayList<ExecutionVaribale>();
@Before("execution(* *.*())") // this will call on before every method call on spring framework Or you can change it depending on which spring package method you can to monitor
public void beforeAnymethodcall(final JoinPoint thisJoinPoint) {
starttime = System.currentTimeMillis();
methodlevleinformation.setMethodname(thisJoinPoint.getStaticPart().getSignature().getName());
}
@After("execution(* *.*())") // this will call on after every method call on spring framework Or you can change it depending on which spring package method you can to monitor
public void afterAnymethodcall(final JoinPoint thisJoinPoint) {
endtime = System.currentTimeMillis();
methodlevleinformation.setDifferenetime((starttime - endtime)/ 1000); // time in secs
liststore.add(methodlevleinformation);
// at last you can iterate list and see the results .Hope this will provide a fine grain information on time taken by spring framework
}
class ExecutionVaribale{
/**
* @return the differenetime
*/
public long getDifferenetime() {
return differenetime;
}
/**
* @param differenetime the differenetime to set
*/
public void setDifferenetime(long differenetime) {
this.differenetime = differenetime;
}
/**
* @return the methodname
*/
public String getMethodname() {
return methodname;
}
/**
* @param methodname the methodname to set
*/
public void setMethodname(String methodname) {
this.methodname = methodname;
}
protected long differenetime;
protected String methodname;
}
}
编辑一个答案:您需要查看记录 bean 创建的流程顺序,这可以通过在服务器启动时从那里添加 log4j.logger.org.springframework=ALL 来实现,您几乎无法获得有关 bean 实例化顺序的信息在容器中继续。同样,您可以自定义 bean 创建。在 log4j.properties 中放
log4j.rootLogger=INFO、标准输出、文件
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.category.org.springframework=ALL
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/folderUnderRootWhereApplicationServerResides/test.log
log4j.appender.file.Encoding=UTF-8
log4j.appender.file.MaxFileSize=40000000KB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
您要记录的包级别 org.springframework.aop 是上层包,config 是 org.springframework.aop log4j.category.org.springframework.aop.config=DEBUG log4j.category.org.springframework.transaction=DEBUG 下的包
请参阅登录 test.log 或 eclipse 控制台中的 folderUnderRootWhereApplicationServerResides 表示如果 tomcat 安装在 D 中,则在 D 驱动器中创建 folderUnderRootWhereApplicationServerResides 文件夹
见
http://www.mkyong.com/logging/log4j-log4j-properties-examples/