本文主要聊一下如何在spring容器启动时,获取有自定义注解的方法信息。
spring-webmvc-4.3.10.RELEASE-sources.jar!/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
implements MatchableHandlerMapping, EmbeddedValueResolverAware {
private boolean useSuffixPatternMatch = true;
private boolean useRegisteredSuffixPatternMatch = false;
private boolean useTrailingSlashMatch = true;
private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager();
private StringValueResolver embeddedValueResolver;
private RequestMappingInfo.BuilderConfiguration config = new RequestMappingInfo.BuilderConfiguration();
//....
}
for(Map.Entry<RequestMappingInfo,HandlerMethod> entry : urlMethodMapping.entrySet()){
RequestMappingInfo info = entry.getKey();
HandlerMethod handlerMethod = entry.getValue();
Method method = handlerMethod.getMethod();
if(!method.isAnnotationPresent(DemoAnno.class)){
continue;
}
DemoAnno demoAnno = method.getAnnotation(DemoAnno.class);
//......
}
通过这个HandlerMethod就可以获取具体的请求的方法,然后利用反射去判断是否有标注自己所需要的注解。
实现ApplicationListener接口,在
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
//.......
}
在onApplicationEvent中去处理 这样就大功告成了
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有