SSM-Spring-17:Spring中aspectJ注解版

释放双眼,带上耳机,听听看~!

 

 

 


1
2
1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2

 

 

 

AspectJ

 

AspectJ
是一个面向切面的框架,它扩展了
Java
语言,定义了
AOP
语法,能够在编译期提供代码的织入

 

**       @AspectJ

AspectJ 5
新增的功能,使用
JDK 5.0
注解技术和正规的
AspectJ
切点表达式语言描述切面**

 

**       Spring
通过集成
AspectJ
实现了以注解的方式定义增强类,大大减少了配置文件中的工作量**

 

**       
使用@AspectJ
,首先要保证所用的
JDK

5.0
或以上版本**

 

注解版案例:

一个接口,一个类,一个普通类,实现注解的

 

ISomeService接口:

 


1
2
3
4
5
6
7
8
9
10
11
12
1package cn.dawn.day19aspectj;
2
3/**
4 * Created by Dawn on 2018/3/8.
5 */
6public interface ISomeService {
7    public void insert();
8    public void delete();
9    public void select();
10    public void update();
11}
12

 

SomeServiceImpl类,上面那个的实现类

 


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
1package cn.dawn.day19aspectj;
2
3
4
5/**
6 * Created by Dawn on 2018/3/8.
7 */
8public class SomeServiceImpl implements ISomeService {
9
10    public void insert() {
11        System.out.println("insert ok");
12    }
13
14    public void delete() {
15        System.out.println("delete ok");
16
17    }
18
19    public void select() {
20        System.out.println("select ok");
21
22    }
23
24    public void update() {
25        System.out.println("update ok");
26        int a=5/0;
27        System.out.println(a);
28
29    }
30}
31

 

他有一个方法中模拟了除零的算数异常,方便一会测试异常增强:

刚才我说的那个实现aspectj注解的类

 


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
1package cn.dawn.day19aspectj;
2
3import org.aspectj.lang.ProceedingJoinPoint;
4import org.aspectj.lang.annotation.*;
5
6/**
7 * Created by Dawn on 2018/3/12.
8 */
9@Aspect
10public class MyAspectJ {
11
12    /*单独拎出来的注解,一会下面用他的方法名可以调用到他的切点表达式*/
13    @Pointcut(value = "execution(* *..day19aspectj.*.select(..))")
14    public void select(){
15
16    }
17    @Pointcut(value = "execution(* *..day19aspectj.*.update(..))")
18    public void update(){
19
20    }
21
22    /*后置增强*/
23    @AfterReturning("execution(* *..day19aspectj.*.select(..))")
24    public void myAfterReturning(){
25        System.out.println("后置增强");
26    }
27
28    /*前置增强*/
29    @Before("execution(* *..day19aspectj.*.insert(..))")
30    public void myBefore(){
31        System.out.println("前置增强");
32    }
33
34    /*异常增强*/
35    @AfterThrowing("execution(* *..day19aspectj.*.update(..))")
36    public void myAfterThrowing(){
37        System.out.println("异常增强");
38    }
39
40    /*最终增强*//*
41    @After("execution(* *..day19aspectj.*.update(..))||execution(* *..day19aspectj.*.select(..))")
42    public void myafter(){
43        System.out.println("我是最终增强");
44    }*/
45
46    /*最终增强*/
47    @After("update()||select()")
48    public void myafter(){
49        System.out.println("我是最终增强");
50    }
51
52    /*环绕增强*/
53    @Around("execution(* *..day19aspectj.*.insert(..))")
54    public void myAround(ProceedingJoinPoint pjp) throws Throwable {
55        System.out.println("环绕增强前");
56        pjp.proceed();
57        System.out.println("环绕增强后");
58    }
59}
60

 

此处要说的只有一个他匹配的时候想要传入多个条件,符合任意一个就对方法增强,只需要用||逻辑或来分割,他上面我多写了一种方式,可以把切点表达式提出来,下面都可以使用,有点像提前声明,而不是用到的时候再声明的感觉

 

单测方法:

 


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
1package cn.dawn.day19aspectj;
2
3
4import org.junit.Test;
5import org.springframework.context.ApplicationContext;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8/**
9 * Created by Dawn on 2018/3/3.
10 */
11public class test20180312 {
12    @Test
13    /*aop代理工厂bean异常增强*/
14    public void t01(){
15        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day19aspectj.xml");
16        ISomeService service = (ISomeService) context.getBean("service");
17
18        try {
19            service.update();
20        }catch (Exception e){
21            e.printStackTrace();
22        }
23        service.delete();
24        service.select();
25        service.insert();
26
27    }
28}
29

 

我知道这块比较繁琐,不太好用语言解释,多练,毕竟博客的方式不如现场和视频,直播等,多练,练的时候理解

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++异常

2022-1-11 12:36:11

安全运维

Redis慢查询日志

2021-12-11 11:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索