fix:An error while loading all controllers

This commit is contained in:
huoyo 2024-09-30 00:12:24 +08:00
parent 8ec2a5b8de
commit e1c168c1c6
3 changed files with 26 additions and 12 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.langpy</groupId> <groupId>cn.langpy</groupId>
<artifactId>ko-time</artifactId> <artifactId>ko-time</artifactId>
<version>2.4.8</version> <version>2.4.9</version>
<name>KoTime</name> <name>KoTime</name>
<description>A springboot tool for tracking the paths of the methods,which can help you find method's performances easily.</description> <description>A springboot tool for tracking the paths of the methods,which can help you find method's performances easily.</description>
<licenses> <licenses>

View File

@ -7,6 +7,7 @@ import cn.langpy.kotime.util.KoUtil;
import cn.langpy.kotime.util.MethodType; import cn.langpy.kotime.util.MethodType;
import org.springframework.aop.MethodMatcher; import org.springframework.aop.MethodMatcher;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor; import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
@ -52,29 +53,42 @@ public class SaveResourceConfig implements CommandLineRunner {
acquireControllers(); acquireControllers();
} }
private void acquireControllers() { private void acquireControllers() {
RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class); try {
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods(); RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
GraphService graphService = GraphService.getInstance(); Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
MethodMatcher methodMatcher = aspectJExpressionPointcutAdvisor.getPointcut().getMethodMatcher(); GraphService graphService = GraphService.getInstance();
for (Map.Entry<RequestMappingInfo, HandlerMethod> methodEntry : handlerMethods.entrySet()) { MethodMatcher methodMatcher = aspectJExpressionPointcutAdvisor.getPointcut().getMethodMatcher();
HandlerMethod handlerMethod = methodEntry.getValue(); for (Map.Entry<RequestMappingInfo, HandlerMethod> methodEntry : handlerMethods.entrySet()) {
boolean matches = methodMatcher.matches(handlerMethod.getMethod(), handlerMethod.getClass()); HandlerMethod handlerMethod = methodEntry.getValue();
if (matches) { boolean matches = methodMatcher.matches(handlerMethod.getMethod(), handlerMethod.getClass());
MethodNode methodNode = toMethodNode(handlerMethod); if (matches) {
graphService.addMethodNode(methodNode); MethodNode methodNode = toMethodNode(handlerMethod);
if (methodNode == null) {
continue;
}
graphService.addMethodNode(methodNode);
}
} }
} catch (BeansException e) {
log.warning("kotime=>An error occured while loading all controllers:"+e.getMessage());
} }
} }
private MethodNode toMethodNode(HandlerMethod method) { private MethodNode toMethodNode(HandlerMethod method) {
Class<?> beanType = method.getBeanType(); Class<?> beanType = method.getBeanType();
RequestMapping requestMapping = beanType.getAnnotation(RequestMapping.class); RequestMapping requestMapping = beanType.getAnnotation(RequestMapping.class);
if (requestMapping == null) {
return null;
}
String[] cvalues = requestMapping.value(); String[] cvalues = requestMapping.value();
String classRoute = ""; String classRoute = "";
if (cvalues != null && cvalues.length > 0) { if (cvalues != null && cvalues.length > 0) {
classRoute = cvalues[0]; classRoute = cvalues[0];
} }
RequestMapping methodAnnotation = method.getMethodAnnotation(RequestMapping.class); RequestMapping methodAnnotation = method.getMethodAnnotation(RequestMapping.class);
if (requestMapping == null) {
return null;
}
String[] mvalues = methodAnnotation.value(); String[] mvalues = methodAnnotation.value();
String methodRoute = ""; String methodRoute = "";
if (mvalues != null && mvalues.length > 0) { if (mvalues != null && mvalues.length > 0) {

View File

@ -1 +1 @@
ko-time.version=2.4.8 ko-time.version=2.4.9