实践-annotation-aop

整理一下以前的学习代码。

Spring Aop

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
<properties>
<junit.version>4.12</junit.version>
<lmbok.version>1.18.4</lmbok.version>
<logback.version>1.2.3</logback.version>
<!-- pom文件需要指定打包编码集,[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lmbok.version}</version>
</dependency>

</dependencies>

基于注解的aop实现

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cn.z201.io.annotation.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author z201.coding@gmail.com
**/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationAop {

String value();

}

package cn.z201.io.annotation.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
* @author z201.coding@gmail.com
**/
@Component
@Aspect
public class LogsAspect {

@Pointcut("@annotation(cn.z201.io.annotation.aop.AnnotationAop)")
private void cutMethod() {

}

/**
* 异常通知:目标方法抛出异常时执行
*/
@AfterThrowing("cutMethod()")
public void afterThrowing(JoinPoint joinPoint) {
System.out.println(" after throwing");
}

/**
* 环绕通知:灵活自由的在目标方法中切入代码
*/
@Before("cutMethod()")
public void around(JoinPoint joinPoint) throws Throwable {
// 获取目标方法的名称
String methodName = joinPoint.getSignature().getName();
// 获取方法传入参数
Object[] params = joinPoint.getArgs();
if (null != params && params.length != 0) {
System.out.println(" method name " + methodName + " args " + params[0]);
}
// 执行源方法
AnnotationAop annotationAop = getDeclaredAnnotation(joinPoint);
System.out.println(" annotationAop value " + annotationAop.value());
}

/**
* 获取方法中声明的注解
*
* @param joinPoint
* @return
* @throws NoSuchMethodException
*/
public AnnotationAop getDeclaredAnnotation(JoinPoint joinPoint) throws NoSuchMethodException {
// 获取方法名
String methodName = joinPoint.getSignature().getName();
// 反射获取目标类
Class<?> targetClass = joinPoint.getTarget().getClass();
// 拿到方法对应的参数类型
Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getParameterTypes();
// 根据类、方法、参数类型(重载)获取到方法的具体信息
Method objMethod = targetClass.getMethod(methodName, parameterTypes);
// 拿到方法定义的注解信息
AnnotationAop annotation = objMethod.getDeclaredAnnotation(AnnotationAop.class);
// 返回
return annotation;
}

}


package cn.z201.io.annotation.aop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
* @author z201.coding@gmail.com
**/
@SpringBootApplication
@EnableAspectJAutoProxy
public class AnnotationApp {

public static void main(String[] args) {
SpringApplication.run(AnnotationApp.class, args);
}

}


package cn.z201.io.annotation.aop;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
* @author z201.coding@gmail.com
**/
@RestController
@RequestMapping
public class HelloController {

@GetMapping("")
@AnnotationAop("Aop")
public Object index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("value", "hello annotationApp ");
return modelAndView.getModelMap();
}
}

输出结果

​ 控制台 annotationAop value Aop

​ 响应 {“value”: “hello annotationApp “}

END