
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/aop
8 http://www.springframework.org/schema/aop/spring-aop.xsd
9 ">
10 <bean id="logUtil" class="com.snails.aop.xml.util.LogUtil" ></bean>
11 <bean id="mathCalculator" class="com.snails.aop.xml.service.MathCalculator" ></bean>
12 <aop:config>
13 <aop:aspect ref="logUtil">
14 <aop:pointcut id="myPoint" expression="execution( Integer com.snails.aop.xml.service.MyCalculator.* (..))"/>
15 <aop:around method="around" pointcut-ref="myPoint"></aop:around>
16 <aop:before method="start" pointcut-ref="myPoint"></aop:before>
17 <aop:after method="logFinally" pointcut-ref="myPoint"></aop:after>
18 <aop:after-returning method="stop" pointcut-ref="myPoint" returning="result"></aop:after-returning>
19 <aop:after-throwing method="logException" pointcut-ref="myPoint" throwing="e"></aop:after-throwing>
20 </aop:aspect>
21 </aop:config>
22 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
23 </beans>


1 public class LogUtil {
2
3 public void myPointCut(){}
4
5 private int start(JoinPoint joinPoint){
6 //获取方法签名
7 Signature signature = joinPoint.getSignature();
8 //获取参数信息
9 Object[] args = joinPoint.getArgs();
10 System.out.println("log---"+signature.getName()+"方法开始执行:参数是"+ Arrays.asList(args));
11 return 100;
12 }
13
14 public static void stop(JoinPoint joinPoint,Object result){
15 Signature signature = joinPoint.getSignature();
16 System.out.println("log---"+signature.getName()+"方法执行结束,结果是:"+result);
17 }
18
19 public static void logException(JoinPoint joinPoint,Exception e){
20 Signature signature = joinPoint.getSignature();
21 System.out.println("log---"+signature.getName()+"方法抛出异常:"+e.getMessage());
22 }
23
24 public static void logFinally(JoinPoint joinPoint){
25 Signature signature = joinPoint.getSignature();
26 System.out.println("log---"+signature.getName()+"方法执行结束。。。。。over");
27
28 }
29
30 public Object around(ProceedingJoinPoint pjp) throws Throwable {
31 Signature signature = pjp.getSignature();
32 Object[] args = pjp.getArgs();
33 Object result = null;
34 try {
35 System.out.println("log---环绕通知start:"+signature.getName()+"方法开始执行,参数为:"+Arrays.asList(args));
36 //通过反射的方式调用目标的方法,相当于执行method.invoke(),可以自己修改结果值
37 result = pjp.proceed(args);
38 // result=100;
39 System.out.println("log---环绕通知stop"+signature.getName()+"方法执行结束");
40 } catch (Throwable throwable) {
41 System.out.println("log---环绕异常通知:"+signature.getName()+"出现异常");
42 throw throwable;
43 }finally {
44 System.out.println("log---环绕返回通知:"+signature.getName()+"方法返回结果是:"+result);
45 }
46 return result;
47 }
48
49 }

1 public class MathCalculator {
2
3 public Integer add(Integer i, Integer j) throws NoSuchMethodException {
4 Integer result = i+j;
5 return result;
6 }
7
8 @Override
9 public String toString() {
10 return "super.toString()";
11 }
12
13 }
1 public static void main(String[] args) throws Exception {
2 ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml");
3 MyCalculator bean = ac.getBean(MyCalculator.class);
4 System.out.println(bean);
5 bean.add(1,1);
6 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)