Skip to content

Commit 64956a0

Browse files
authored
Merge pull request #32 from yinjihuan/encrypt1.1
AOP注解扫描失效问题解决
2 parents 6e162db + e7c2395 commit 64956a0

File tree

3 files changed

+67
-29
lines changed

3 files changed

+67
-29
lines changed

encrypt-core/src/main/java/com/cxytiandi/encrypt/springboot/init/ApiEncryptDataInit.java

+33-28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.beans.BeansException;
1111
import org.springframework.context.ApplicationContext;
1212
import org.springframework.context.ApplicationContextAware;
13+
import org.springframework.core.annotation.AnnotationUtils;
1314
import org.springframework.stereotype.Controller;
1415
import org.springframework.util.StringUtils;
1516
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -59,17 +60,19 @@ private void initData(Map<String, Object> beanMap) {
5960
Class<?> clz = bean.getClass();
6061
Method[] methods = clz.getMethods();
6162
for (Method method : methods) {
62-
if (method.isAnnotationPresent(Encrypt.class)) {
63-
// 注解中的URI优先级高
64-
String uri = method.getAnnotation(Encrypt.class).value();
63+
Encrypt encrypt = AnnotationUtils.findAnnotation(method, Encrypt.class);
64+
if (encrypt != null) {
65+
// 注解中的URI优先级高
66+
String uri = encrypt.value();
6567
if (!StringUtils.hasText(uri)) {
6668
uri = getApiUri(clz, method);
6769
}
6870
logger.debug("Encrypt URI: {}", uri);
6971
responseEncryptUriList.add(uri);
70-
}
71-
if (method.isAnnotationPresent(Decrypt.class)) {
72-
String uri = method.getAnnotation(Decrypt.class).value();
72+
}
73+
Decrypt decrypt = AnnotationUtils.findAnnotation(method, Decrypt.class);
74+
if (decrypt != null) {
75+
String uri = decrypt.value();
7376
if (!StringUtils.hasText(uri)) {
7477
uri = getApiUri(clz, method);
7578
}
@@ -85,42 +88,44 @@ private String getApiUri(Class<?> clz, Method method) {
8588
String methodType = "";
8689
StringBuilder uri = new StringBuilder();
8790

88-
if (clz.isAnnotationPresent(RequestMapping.class)) {
89-
uri.append(formatUri(clz.getAnnotation(RequestMapping.class).value()[0]));
91+
RequestMapping reqMapping = AnnotationUtils.findAnnotation(clz, RequestMapping.class);
92+
if (reqMapping != null) {
93+
uri.append(formatUri(reqMapping.value()[0]));
9094
}
9195

92-
if (method.isAnnotationPresent(GetMapping.class)) {
93-
96+
GetMapping getMapping = AnnotationUtils.findAnnotation(method, GetMapping.class);
97+
PostMapping postMapping = AnnotationUtils.findAnnotation(method, PostMapping.class);
98+
RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
99+
PutMapping putMapping = AnnotationUtils.findAnnotation(method, PutMapping.class);
100+
DeleteMapping deleteMapping = AnnotationUtils.findAnnotation(method, DeleteMapping.class);
101+
102+
if (getMapping != null) {
94103
methodType = HttpMethodTypePrefixConstant.GET;
95-
uri.append(formatUri(method.getAnnotation(GetMapping.class).value()[0]));
104+
uri.append(formatUri(getMapping.value()[0]));
96105

97-
} else if (method.isAnnotationPresent(PostMapping.class)) {
98-
106+
} else if (postMapping != null) {
99107
methodType = HttpMethodTypePrefixConstant.POST;
100-
uri.append(formatUri(method.getAnnotation(PostMapping.class).value()[0]));
101-
102-
} else if (method.isAnnotationPresent(RequestMapping.class)) {
103-
104-
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
105-
RequestMethod m = requestMapping.method()[0];
106-
methodType = m.name().toLowerCase() + ":";
107-
uri.append(formatUri(requestMapping.value()[0]));
108+
uri.append(formatUri(postMapping.value()[0]));
108109

109-
} else if (method.isAnnotationPresent(PutMapping.class)) {
110-
110+
} else if (putMapping != null) {
111111
methodType = HttpMethodTypePrefixConstant.PUT;
112-
uri.append(formatUri(method.getAnnotation(PutMapping.class).value()[0]));
112+
uri.append(formatUri(putMapping.value()[0]));
113113

114-
} else if (method.isAnnotationPresent(DeleteMapping.class)) {
115-
114+
} else if (deleteMapping != null) {
116115
methodType = HttpMethodTypePrefixConstant.DELETE;
117-
uri.append(formatUri(method.getAnnotation(DeleteMapping.class).value()[0]));
116+
uri.append(formatUri(deleteMapping.value()[0]));
118117

119-
}
118+
} else if (requestMapping != null) {
119+
RequestMethod m = requestMapping.method()[0];
120+
methodType = m.name().toLowerCase() + ":";
121+
uri.append(formatUri(requestMapping.value()[0]));
122+
123+
}
120124

121125
if (StringUtils.hasText(this.contextPath)) {
122126
return methodType + this.contextPath + uri.toString();
123127
}
128+
124129
return methodType + uri.toString();
125130
}
126131

encrypt-springboot-example/pom.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@
3232
<artifactId>jackson-dataformat-xml</artifactId>
3333
</dependency>
3434
<dependency>
35-
<groupId>com.cxytiandi</groupId>
35+
<groupId>com.cxytiandi</groupId>
3636
<artifactId>monkey-api-encrypt-core</artifactId>
3737
<version>1.1-SNAPSHOT</version>
3838
</dependency>
3939
<dependency>
4040
<groupId>org.springframework.boot</groupId>
4141
<artifactId>spring-boot-starter-freemarker</artifactId>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-aop</artifactId>
46+
</dependency>
4347
</dependencies>
4448
<build>
4549
<plugins>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.cxytiandi.encrypt_springboot_example.aspect;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
import org.aspectj.lang.annotation.Around;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.springframework.core.Ordered;
7+
import org.springframework.core.annotation.Order;
8+
import org.springframework.stereotype.Component;
9+
10+
/**
11+
* 使用切面模拟注解扫描不到问题
12+
* @author yinjihuan
13+
*
14+
*/
15+
//@Component
16+
@Aspect
17+
@Order(value = Ordered.HIGHEST_PRECEDENCE)
18+
public class ApiLimitAspect {
19+
20+
@Around("execution(* com.cxytiandi.encrypt_springboot_example.controller.*.*(..))")
21+
public Object around(ProceedingJoinPoint joinPoint) {
22+
try {
23+
return joinPoint.proceed();
24+
} catch (Throwable e) {
25+
e.printStackTrace();
26+
}
27+
return null;
28+
}
29+
}

0 commit comments

Comments
 (0)