hooyantsing's Blog

第111次课程_Spring_AOP

字数统计: 773阅读时长: 4 min
2020/02/23

源辰76班

第111次课程

2020.02.23

内容

Spring_AOP

项目路径:

**1.**AOP [注解方式]配置

前置、后置、返回和异常增强

LogAdviceAnno.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.yc.spring.bank;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import  org.aspectj.lang.annotation.AfterReturning;
import  org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import  org.springframework.stereotype.Component;
@Component
@Aspect
public class LogAdviceAnno {
    
    //定义切点方法
    @Pointcut("execution (*  com.yc.spring.bank.biz.*.*(..))")
    public void myPc() {}
    
    @Pointcut("execution (*  com.yc.spring.bank.biz.*.*(..))")
    public void myPc1() {}
    
    //前置增强
    @Before("myPc()")
    public void before(JoinPoint jp) {
       System.out.println("Anno 方法签名" +  jp.getSignature());
       System.out.println("Anno 方法参数列表" + Arrays.toString(jp.getArgs()));
       System.out.println("===== Anno  before =====");
    }
    
    //后置增强
    @After("myPc()")
    public void after(JoinPoint jp) {
       System.out.println("===== Anno after  =====");
    }
    
    //返回增强:方法正常执行后,才调用,返回增强要指定业务方法返回的值,对应的参数
    @AfterReturning(value="myPc()",returning="ret")
    public void afterReturning(JoinPoint  jp,Object ret) {
       System.out.println(ret);
       System.out.println("===== Anno  afterReturning =====");
    }
    
    //异常增强
    @AfterThrowing(value="myPc()",throwing="e")
    public void afterTrowing(JoinPoint  jp,Throwable e) {
       System.out.println(e);
       System.out.println("===== Anno  afterTrowing =====");
    }
}

2.环绕增强

功能强大,集成了前四种增强方式。

LogAdviceAnno.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.yc.spring.bank;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import  org.aspectj.lang.annotation.AfterReturning;
import  org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import  org.springframework.stereotype.Component;
@Component
@Aspect
public class LogAdviceAnno {
    
    //定义切点方法
    @Pointcut("execution (*  com.yc.spring.bank.biz.*.*(..))")
    public void myPc() {}
    
    @Pointcut("execution (*  com.yc.spring.common.dao.*.*(..))")
    public void myPc1() {}
    
    @Pointcut("execution (*  com.yc.spring.bank.action.*.*(..))")
    public void myPc2() {}
    
    //前置增强
    @Before("myPc1()")
    public void before(JoinPoint jp) {
       System.out.println("Anno 方法签名" +  jp.getSignature());
       System.out.println("Anno 方法参数列表" + Arrays.toString(jp.getArgs()));
       System.out.println("===== Anno  before =====");
    }
    
    //后置增强
    @After("myPc()")
    public void after(JoinPoint jp) {
       System.out.println("===== Anno after  =====");
    }
    
    //返回增强:方法正常执行后,才调用,返回增强要指定业务方法返回的值,对应的参数
    @AfterReturning(value="myPc()",returning="ret")
    public void afterReturning(JoinPoint  jp,Object ret) {
       System.out.println(ret);
       System.out.println("===== Anno  afterReturning =====");
    }
    
    //异常增强
    @AfterThrowing(value="myPc()",throwing="e")
    public void afterTrowing(JoinPoint  jp,Throwable e) {
       System.out.println(e);
       System.out.println("===== Anno  afterTrowing =====");
    }
    
    /*
     * 环绕增强:
     * 性能监控
     * 事务管理
     *
     * ProceedingoinPoint 环绕增强特有的连接点
     * 环绕增强的业务的执行,由环绕增强特有的连接点对象来控制执行
     */
    @Around("myPc2()")
    public Object around(ProceedingJoinPoint  pjp) {
       try {
           long starttime =  System.currentTimeMillis();
           System.out.println("=====环绕增强  before =====");
           Object ret = pjp.proceed(); //执行业务方法并返回结果
           System.out.println("共耗时:" +  (System.currentTimeMillis() - starttime));
           System.out.println("=====环绕增强  afterReturning =====");
           return ret;
       } catch (Throwable e) {
           System.out.println("=====环绕增强  afterThrowing =====");
           return null;
       } finally {
           System.out.println("=====环绕增强  after =====");
       }
    }
}

3.JDK动态代理 和 CGLIB动态代理

Desktop.zip

CATALOG