Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AOP注解扫描失效问题解决 #32

Merged
merged 1 commit into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand Down Expand Up @@ -59,17 +60,19 @@ private void initData(Map<String, Object> beanMap) {
Class<?> clz = bean.getClass();
Method[] methods = clz.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Encrypt.class)) {
// 注解中的URI优先级高
String uri = method.getAnnotation(Encrypt.class).value();
Encrypt encrypt = AnnotationUtils.findAnnotation(method, Encrypt.class);
if (encrypt != null) {
// 注解中的URI优先级高
String uri = encrypt.value();
if (!StringUtils.hasText(uri)) {
uri = getApiUri(clz, method);
}
logger.debug("Encrypt URI: {}", uri);
responseEncryptUriList.add(uri);
}
if (method.isAnnotationPresent(Decrypt.class)) {
String uri = method.getAnnotation(Decrypt.class).value();
}
Decrypt decrypt = AnnotationUtils.findAnnotation(method, Decrypt.class);
if (decrypt != null) {
String uri = decrypt.value();
if (!StringUtils.hasText(uri)) {
uri = getApiUri(clz, method);
}
Expand All @@ -85,42 +88,44 @@ private String getApiUri(Class<?> clz, Method method) {
String methodType = "";
StringBuilder uri = new StringBuilder();

if (clz.isAnnotationPresent(RequestMapping.class)) {
uri.append(formatUri(clz.getAnnotation(RequestMapping.class).value()[0]));
RequestMapping reqMapping = AnnotationUtils.findAnnotation(clz, RequestMapping.class);
if (reqMapping != null) {
uri.append(formatUri(reqMapping.value()[0]));
}

if (method.isAnnotationPresent(GetMapping.class)) {

GetMapping getMapping = AnnotationUtils.findAnnotation(method, GetMapping.class);
PostMapping postMapping = AnnotationUtils.findAnnotation(method, PostMapping.class);
RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
PutMapping putMapping = AnnotationUtils.findAnnotation(method, PutMapping.class);
DeleteMapping deleteMapping = AnnotationUtils.findAnnotation(method, DeleteMapping.class);

if (getMapping != null) {
methodType = HttpMethodTypePrefixConstant.GET;
uri.append(formatUri(method.getAnnotation(GetMapping.class).value()[0]));
uri.append(formatUri(getMapping.value()[0]));

} else if (method.isAnnotationPresent(PostMapping.class)) {

} else if (postMapping != null) {
methodType = HttpMethodTypePrefixConstant.POST;
uri.append(formatUri(method.getAnnotation(PostMapping.class).value()[0]));

} else if (method.isAnnotationPresent(RequestMapping.class)) {

RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
RequestMethod m = requestMapping.method()[0];
methodType = m.name().toLowerCase() + ":";
uri.append(formatUri(requestMapping.value()[0]));
uri.append(formatUri(postMapping.value()[0]));

} else if (method.isAnnotationPresent(PutMapping.class)) {

} else if (putMapping != null) {
methodType = HttpMethodTypePrefixConstant.PUT;
uri.append(formatUri(method.getAnnotation(PutMapping.class).value()[0]));
uri.append(formatUri(putMapping.value()[0]));

} else if (method.isAnnotationPresent(DeleteMapping.class)) {

} else if (deleteMapping != null) {
methodType = HttpMethodTypePrefixConstant.DELETE;
uri.append(formatUri(method.getAnnotation(DeleteMapping.class).value()[0]));
uri.append(formatUri(deleteMapping.value()[0]));

}
} else if (requestMapping != null) {
RequestMethod m = requestMapping.method()[0];
methodType = m.name().toLowerCase() + ":";
uri.append(formatUri(requestMapping.value()[0]));

}

if (StringUtils.hasText(this.contextPath)) {
return methodType + this.contextPath + uri.toString();
}

return methodType + uri.toString();
}

Expand Down
6 changes: 5 additions & 1 deletion encrypt-springboot-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>com.cxytiandi</groupId>
<groupId>com.cxytiandi</groupId>
<artifactId>monkey-api-encrypt-core</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.cxytiandi.encrypt_springboot_example.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
* 使用切面模拟注解扫描不到问题
* @author yinjihuan
*
*/
//@Component
@Aspect
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class ApiLimitAspect {

@Around("execution(* com.cxytiandi.encrypt_springboot_example.controller.*.*(..))")
public Object around(ProceedingJoinPoint joinPoint) {
try {
return joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
}