Spring-Aop

<!--AOP 面向切面编程-->
        在xml中启用AspectJ
        <context:component-scan base-package="com.wsl.aop"></context:component-scan>
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
 1      //编写一个抽象接口calcinterface
 2         public interface calcinterface
 3         {
 4             public int add(int x , int y);
 5             public int sub(int x , int y);
 6             public int mul(int x , int y);
 7             public int div(int x , int y);
 8         }
 9         
10         编写一个calcimpl类实现calcinterface接口
11         @Component(value ="calc")
12         public class calcimpl implements calcinterface 
13         {
14             ..............//实现接口方法
15         }
16         
17         编写日志类
18         @Order(2)          //指定切面的优先级 , 值越小优先级越高,如果同时两个切面对应一个类, 那么在
19                         //这两个切面中@Order(X) x的值越小 , 优先级 越高 , 先执行这个切面
20         @Aspect          //AspectJ注解
21         @Component        //IOC bean注解
22         public class calcproxy 
23         {
24             @Before("execution(public int com.wsl.aop.calcinterface.*(..))")//注意,此处扑捉的是接口 , 当中的公共 返回int的全部方法,也可以写参数,或函数名明确捕捉。
25             public void  before(JoinPoint joinpoint)
26             {
27                 String methodname = joinpoint.getSignature().getName();//得到事件名
28                 System.err.println("before message "+methodname +"  method 。。。。。"+ ArraysAsList(joinpoint.getArgs())); //输出信息+全部参数
29             }
30 
31             @After消息 :使用和上面一样 , //不管是否出错,都调用
32             @AfterReturning://正确返回时调用
33                 @AfterReturning(value = "execution(public int com.wsl.aop.calcinterface.*(..))",returning = "result")
34                 public void afterrerutn(JoinPoint joinpoint , Object result)
35             @AfterThrowing//异常通知,在执行方法时抛异常调用
36                 @AfterThrowing(value = "execution(public int com.wsl.aop.calcinterface.*(..))",throwing = "e")
37                 public void throwmethod(JoinPoint joinpoint  ,Exception e)
38             @Around:不能和之前的四个消息共存
39                 @Around("execution(public int com.wsl.aop.calcinterface.*(..))")
40                 public Object around_method(ProceedingJoinPoint point )
41                 {
42                     Object resultObject = null;
43         
44                     try {
45                         System.out.println("around message" + Arrays.asList(point.getArgs()));
46                         System.out.println("before message");
47                         resultObject = point.proceed();
48                         System.out.println("after  message");
49                         
50                     } catch (Throwable e) {
51                         // TODO Auto-generated catch block
52                         System.out.println("throw  message "+ e);
53                     }
54                     System.out.println("return  message");
55                     return resultObject;
56                 }
57         }
58         
59         重用切点表达式
60         定义一个方法 , 用于申明切入点表达式。 一般地 , 该方法再不需要加入其他代码 , 如:
61         
62         @Pointcut("execution(public int com.wsl.aop.calcinterface.*(..))")
63         public void getJoinPoint(){}
64         
65         在使用时
66         @Around("getJoinPoint()")
67         public Object around_method(ProceedingJoinPoint point )
68 69         @AfterReturning(value = "getJoinPoint()",returning = "result")
70         public void afterrerutn(JoinPoint joinpoint , Object result)
71         
72         XML方式AOP配置 略......
原文地址:https://www.cnblogs.com/wslblog/p/4798267.html