【Spring AOP 详解】
此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题。最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP来解决。一方面是为了以更加灵活的方式来解决问题,另一方面是借此机会深入学习SpringAOP相关的内容。本文是权当本人的自己AOP学习笔记,以下需求不用AOP肯定也能解决,至于是否牵强附会,仁者见仁智者见智。
对部分函数的调用进行日志记录,用于观察特定问题在运行过程中的函数调用情况
监控部分重要函数,若抛出指定的异常,需要以短信或邮件方式通知相关人员
金控部分重要函数的执行时间
事实上,以上需求没有AOP也能搞定,只是在实现过程中比较郁闷摆了。
需要打印日志的函数分散在各个包中,只能找到所有的函数体,手动添加日志。然而这些日志都是临时的,待问题解决之后应该需要清除打印日志的代码,只能再次手动清除^_^!
类似1的情况,需要捕获异常的地方太多,如果手动添加时想到很可能明天又要手动清除,只能再汗。OK,该需求相对比较固定,属于长期监控的范畴,并不需求临时添加后再清除。然而,客户某天要求,把其中20%的异常改为短信提醒,剩下的80%改用邮件提醒。改之,两天后,客户抱怨短信太多,全部改成邮件提醒…
该需求通常用于监控某些函数的执行时间,用以判断系统执行慢的瓶颈所在。瓶颈被解决之后,烦恼同情况1
终于下定决心,采用AOP来解决!代码如下:
切面类TestAspect
[java]
package com.spring.aop;
/**
* 切面
*
*/
public class TestAspect {
public void doAfter(JoinPoint jp) {
System.out.println("log Ending method: "
+ jp.getTarget()。getClass()。getName() + "."
+ jp.getSignature()。getName());
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("process time: " + time + " ms");
return retVal;
}
public void doBefore(JoinPoint jp) {
System.out.println("log Begining method: "
+ jp.getTarget()。getClass()。getName() + "."
+ jp.getSignature()。getName());
}
public void doThrowing(JoinPoint jp, Throwable ex) {
System.out.println("method " + jp.getTarget()。getClass()。getName()
+ "." + jp.getSignature()。getName() + " throw exception");
System.out.println(ex.getMessage());
}
private void sendEx(String ex) {
//TODO 发送短信或邮件提醒
}
}
package com.spring.aop;
/**
* 切面
*
*/
public class TestAspect {
public void doAfter(JoinPoint jp) {
System.out.println("log Ending method: "
+ jp.getTarget()。getClass()。getName() + "."
+ jp.getSignature()。getName());
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("process time: " + time + " ms");
return retVal;
}
public void doBefore(JoinPoint jp) {
System.out.println("log Begining method: "
+ jp.getTarget()。getClass()。getName() + "."
+ jp.getSignature()。getName());
}
public void doThrowing(JoinPoint jp, Throwable ex) {
System.out.println("method " + jp.getTarget()。getClass()。getName()
+ "." + jp.getSignature()。getName() + " throw exception");
System.out.println(ex.getMessage());
}
private void sendEx(String ex) {
//TODO 发送短信或邮件提醒
}
}
[java]
package com.spring.service;
/**
* 接口A
*/
public interface AService {
public void fooA(String _msg);
public void barA();
}
package com.spring.service;
/**
* 接口A
*/
public interface AService {
public void fooA(String _msg);
public void barA();
} [java]
package com.spring.service;
/**
*接口A的实现类
*/
public class AServiceImpl implements AService {
public void barA() {
System.out.println("AServiceImpl.barA()");
}
public void fooA(String _msg) {
System.out.println("AServiceImpl.fooA(msg:"+_msg+")");
}
}
package com.spring.service;
/**
*接口A的实现类
*/
public class AServiceImpl implements AService {
public void barA() {
System.out.println("AServiceImpl.barA()");
}
public void fooA(String _msg) {
System.out.println("AServiceImpl.fooA(msg:"+_msg+")");
}
}
[java]
package com.spring.service;
/**
* Service类B
*/
public class BServiceImpl {
public void barB(String _msg, int _type) {
System.out.println("BServiceImpl.barB(msg:"+_msg+" type:"+_type+")");