spring aop 4

public class LogAspect {
private static Logger logger = Logger.getLogger(LogAspect.class);
/**
* <p>
* 功能实现描述:最简单的情况
* </p>
*/
public void log() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String rightnow = sdf.format(sdf);
System.out.println(rightnow + " *******Log*********");
}


/**
* 前置通知(@Before):在某连接点(join point)之前执行的通知,但这个通知不能阻止连接点前的执行(除非它抛出一个异常);
* 有参无返回值的方法;

* @param joinPoint
*/
public void before(JoinPoint joinPoint) {
System.out.println("======================前置通知======================");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String rightnow = sdf.format(new Date());
Signature signature = joinPoint.getSignature();
String operateMethodName = signature.getName();
// 获取目标方法体参数
List<Object> list = Arrays.asList(joinPoint.getArgs());


System.out.println(rightnow + "执行了【" + operateMethodName + "方法开始执行......】");
System.out.println("******参数" + list + "******");
}


/**
* 后通知(@After):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)

* @param joinPoint
*/
public void after(JoinPoint joinPoint) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String rightnow = sdf.format(new Date());
System.out.println("======================后置通知======================");
Signature signature = joinPoint.getSignature();
String operateMethodName = signature.getName();
// 获取目标方法体参数
List<Object> list = Arrays.asList(joinPoint.getArgs());
System.out.println(rightnow + "执行了【" + operateMethodName + "方法结束......】");
System.out.println("******参数" + list + "******");
}


/**
* 返回后通知(@AfterReturning):在某连接点(join point)正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回;
* 有参并有返回值的方法; 在切面方法参数中加入Object result,用于接受返回通知
* 的返回结果。如果目标方法方法是void返回类型则返回NULL

* @param joinPoint
*/
public void afterReturning(JoinPoint joinPoint, Object returnObj) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String rightnow = sdf.format(new Date());
System.out.println("======================返回后通知======================");
Signature signature = joinPoint.getSignature();
String operateMethodName = signature.getName();
System.out.println(rightnow + "执行了【" + operateMethodName + "方法正常执行结束......】" + "【返回结果:" + returnObj + "】");
System.out.println("√√√√√√√√√√√√√√√√√√√√√√方法结束√√√√√√√√√√√√√√√√√√√√√√");
}


/**
* 抛出异常后通知(@AfterThrowing):方法抛出异常退出时执行的通知

* @param joinPoint
*/
public void afterThrowing(JoinPoint joinPoint, Exception exception) {
System.out.println("======================抛出异常后通知======================");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String rightnow = sdf.format(new Date());
Signature signature = joinPoint.getSignature();
String operateMethodName = signature.getName();
// 获取目标方法体参数
// List<Object> list = Arrays.asList(joinPoint.getArgs());
System.out.println(rightnow + "执行了【" + operateMethodName + "方法发生异常......】" + "【异常报告:" + exception + "】");
System.out.println("xxxxxxxxxxxxxxxxxx方法发生异常结束xxxxxxxxxxxxxxxxxx");
}


/**
* 环绕通知(@Around):包围一个连接点(join point)的通知,如方法调用。
* 这是最强大的一种通知类型,环绕通知可以在方法调用前后完成自定义的行为,它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行;
* 环绕通知需要携带ProceedingJoinPoint 这个类型的参数; 环绕通知类似于动态代理的全过程;
* ProceedingJoinPoint类型的参数可以决定是否执行目标函数; 环绕通知必须有返回值;

* @param joinPoint
*/
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("======================环绕通知======================");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String rightnow = sdf.format(new Date());
Object result = null;
Object classMethod = proceedingJoinPoint.getSignature();
Signature signature = proceedingJoinPoint.getSignature();
String operateMethodName = signature.getName();
try {
// 前置通知
System.out.println(rightnow + "环绕通知执行了【" + classMethod
+ "方法开始执行......】");
// 执行目标方法
result = proceedingJoinPoint.proceed();
// 返回通知
System.out.println(rightnow + "环绕通知正常执行【" + classMethod
+ "方法完毕......】" + "【返回结果:】" + result);
} catch (Throwable e) {
e.printStackTrace();
// 异常通知
System.out.println(rightnow + "环绕通知非正常执行【" + classMethod
+ "方法完毕,抛出异常......】" + "【返回异常:】" + e);
}
// 后置通知
System.out.println(rightnow + "环绕通知执行【" + classMethod + "方法完毕】");
return result;

}

}




<!-- Spring AOP实现日志记录-->
<bean id="logAspect" class="com.xxxxx.web.interceptor.LogAspect"/>


<aop:config>
<!-- order参数是用来控制aop通知的优先级,值越小,优先级越高 -->
  <aop:aspect id="logAspect" ref="logAspect" order="1">
  <aop:pointcut id="allAddMethod1" expression="execution(* com.xxxxx.web.service.impl.ProjectServiceImpl.find*(..)) || execution(* com.xxxxxxxxxx.web.service.impl.ProjectServiceImpl.del*(..))"/>
  <aop:before method="before" pointcut-ref="allAddMethod1"/> 
  </aop:aspect>
</aop:config>
<!-- <aop:config>
  <aop:aspect id="logAspect" ref="logAspect" order="1">
  <aop:pointcut id="allAddMethod1" expression="execution(* com.xxxxx.web.service.impl.ProjectServiceImpl.find*(..)) || execution(* com.xxxxxxxxxxx.web.service.impl.ProjectServiceImpl.del*(..))"/>
  <aop:after method="after" pointcut-ref="allAddMethod1"/>
  </aop:aspect>
</aop:config> -->
<!-- <aop:config>
  <aop:aspect id="logAspect" ref="logAspect" order="1">

<aop:pointcut id="allAddMethod1" expression="execution(* com.xxxxxxxxxx.web.service.impl.ProjectServiceImpl.find*(..)) || execution(* com.xxxxxxxxxxx.web.service.impl.ProjectServiceImpl.del*(..))"/>
  <aop:after-returning method="afterReturning" returning="returnObj" pointcut-ref="allAddMethod1"/>
  </aop:aspect>
</aop:config> -->
<!-- <aop:config>
<aop:aspect id="logAspect" ref="logAspect" order="1">
  <aop:pointcut id="allAddMethod1" expression="execution(* com.xxxxxx.web.service.impl.ProjectServiceImpl.find*(..)) || execution(* com.xxxxxxxxxxxxx.web.service.impl.ProjectServiceImpl.del*(..))"/>
  <aop:after-throwing method="afterThrowing" throwing="exception" pointcut-ref="allAddMethod1"/>
  </aop:aspect>
</aop:config> -->
<!-- <aop:config>
  <aop:aspect id="logAspect" ref="logAspect" order="1">
  <aop:pointcut id="allAddMethod1" expression="execution(* com.xxxxxxxxxx.web.service.impl.ProjectServiceImpl.find*(..)) || execution(* com.xxxxxxxxxxxx.web.service.impl.ProjectServiceImpl.del*(..))"/>
  <aop:around method="around" pointcut-ref="allAddMethod1"/> 
  </aop:aspect>
</aop:config> -->


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值