AOP中环绕通知的写法
Posted newcityboy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AOP中环绕通知的写法相关的知识,希望对你有一定的参考价值。
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中环绕通知的写法的主要内容,如果未能解决你的问题,请参考以下文章
spring AOP中,如何获被取环绕通知的方法的返回类型?