最近想写个基于spring的日志记录COM,首先浮现出来的肯定是AOP
动手开始写
private void writeLog() {
System.out.println(new Date().toString()+"执行了"+某某方法+"操作的数据为:"+某某数据);
}
问题就在这,这个方法名和参数怎么来获取呢?
查询了下资料,原来这些信息被spring自动封装在了JoinPoint里面
private void writeLog(JoinPoint joinPoint) {
//获取传入的参数
Object[] args = joinPoint.getArgs();
for (int i=0; i<args.length; i++) {
System.out.println(args[i]);
}
//获取被AOP的方法
String methodName=joinPoint.getSignature().getName();
}
最后还去思考了下,基于动态代理,基于annotation,基于schema这3种AOP实现方式
动态代理明显被schema替代,annotation和schema算是各有千秋吧!