码迷,mamicode.com
首页 > 其他好文 > 详细

AOP中环绕通知的写法

时间:2019-11-23 19:53:08      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:null   exp   throwable   err   odi   city   auth   package   对象   

package com.hope.utils;

import org.aspectj.lang.ProceedingJoinPoint;

/**
* @author newcityman
* @date 2019/11/22 - 23:29
* 记录日志的类
*/
public class Logger {
/**
* 前置通知
*/
public void beforePrintLog(){
System.out.println("前置通知");
}

/**
* 后置通知
*/
public void afterReturningPrintLog(){
System.out.println("后置通知");
}

/**
* 异常通知
*/
public void afterThrowingPrintLog(){
System.out.println("异常通知");
}

/**
* 最终通知
*/
public void afterPrintLog(){
System.out.println("最终通知");
}

/**
* 环绕通知
*/
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
System.out.println("前置通知");
Object[] args = pjp.getArgs();
rtValue= pjp.proceed(args);
System.out.println("后置通知");
return rtValue;
} catch (Throwable t) {
System.out.println("异常通知");
throw new RuntimeException(t);
}finally {
System.out.println("最终通知");
}

}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<!--配置spring的ioc,把service对象配置进来-->
<bean id="accountService" class="com.hope.service.impl.AccountService"></bean>
<!--配置logger类-->
<bean id="logger" class="com.hope.utils.Logger"></bean>
<!--配置AOP-->
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<aop:pointcut id="pt1" expression="execution(* *..*.*(..))"/>
<!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
<!--<aop:before method="printLog" pointcut="execution(public void com.hope.service.impl.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(void com.hope.service.impl.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(* com.hope.service.impl.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(* *.*.*.*.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(* *.*.*.*.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(* *..AccountService.saveAccount())"/>-->
<!--<aop:before method="printLog" pointcut="execution(* *..*.*())"/>-->
<!--<aop:before method="beforePrintLog" pointcut-ref="pt1"/>-->
<!--<aop:after-returning method="afterReturningPrintLog" pointcut="execution(* *..*.*(..))"/>-->
<!--<aop:after-throwing method="afterThrowingPrintLog" pointcut="execution(* *..*.*(..))"/>-->
<!--<aop:after method="afterPrintLog" pointcut-ref="pt1"/>-->
<!--<aop:before method="printLog" pointcut="execution(* *..*.*(..))"/>-->
<aop:around method="aroundPrintLog" pointcut-ref="pt1"/>
</aop:aspect>
</aop:config>
</beans>


AOP中环绕通知的写法

标签:null   exp   throwable   err   odi   city   auth   package   对象   

原文地址:https://www.cnblogs.com/newcityboy/p/11919321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!