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>
<artifactId>ko-time</artifactId>
<version>2.4.8</version>
<version>2.4.9</version>
<name>KoTime</name>
<description>A springboot tool for tracking the paths of the methods,which can help you find method's performances easily.</description>
<licenses>

View File

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

View File

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