1、aop.xml配置文件
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>
2、logUtil实现
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 }
3、创建被代理类MathCalculator
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 }
4、测试入口
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 }标签:示例,Spring,System,源码,result,signature,println,public,out From: https://www.cnblogs.com/RunningSnails/p/17008895.html