JAVA全系列 教程
3762个小节阅读:7090.6k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
package com.bjsxt.advice;
import org.aspectj.lang.ProceedingJoinPoint;
/*
如果希望获取切入点方法的参数,推荐把通知的参和切入点的参数写成一致。
*/
public class MyAdvice2 {
public void mybefore(int id1, boolean bool1){
System.out.println("前置通知"+id1+","+bool1);
}
public void myafter(int id1, boolean bool1){
System.out.println("后置:"+id1+","+bool1);
}
public void myafter2(int id1, boolean bool1){
System.out.println("后置2:"+id1+","+bool1);
}
public Object myaround(ProceedingJoinPoint pjp,int id1, boolean bool1) throws Throwable {
System.out.println("环绕前置");
Object result = pjp.proceed();
System.out.println("环绕后置");
return result;
}
public void mythrow(Exception e,int id1, boolean bool1){
System.out.println("异常通知:"+e.getMessage()+",id:"+id1+",bool1:"+bool1);
}
}
在配置文件中配置
xxxxxxxxxx
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.bjsxt.mapper,com.bjsxt.service.impl"></context:component-scan>
<bean id="myadvice2" class="com.bjsxt.advice.MyAdvice2"></bean>
<aop:config>
<!-- ref引用自定义通知类对应bean的id -->
<aop:aspect ref="myadvice2">
<!-- args写在execution括号外面 -->
<!-- args括号内的名称和通知中方法参数名相同-->
<aop:pointcut id="mypoint" expression="execution(* com.bjsxt.service.impl.PeopleServiceImpl.test(int,boolean)) and args(id1,bool1)"/>
<aop:before method="mybefore" pointcut-ref="mypoint" arg-names="id1,bool1"></aop:before>
<aop:after-returning method="myafter" pointcut-ref="mypoint" arg-names="id1,bool1"></aop:after-returning>
<aop:after method="myafter2" pointcut-ref="mypoint" arg-names="id1,bool1"></aop:after>
<!-- names里面取值要和通知中方法参数名完全一致。pjp由Spring帮助自动注入进来 -->
<aop:around method="myaround" pointcut-ref="mypoint" arg-names="pjp,id1,bool1" ></aop:around>
<!-- 不要忘记通过throwing属性声明异常参数名称-->
<aop:after-throwing method="mythrow" pointcut-ref="mypoint" arg-names="e,id1,bool1" throwing="e"></aop:after-throwing>
</aop:aspect>
</aop:config>
</beans>