本文主要聊一下如何在spring容器啓動時,獲取有自定義註解的方法信息。java
spring-webmvc-4.3.10.RELEASE-sources.jar!/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.javaweb
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(); //.... }
/** * Return a (read-only) map with all mappings and HandlerMethod's. */ public Map<T, HandlerMethod> getHandlerMethods() { this.mappingRegistry.acquireReadLock(); try { return Collections.unmodifiableMap(this.mappingRegistry.getMappings()); } finally { this.mappingRegistry.releaseReadLock(); } }
主要用到這個getHandlerMethods方法spring
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就能夠獲取具體的請求的方法,而後利用反射去判斷是否有標註本身所須要的註解。mvc
實現ApplicationListener<ContextRefreshedEvent>接口,在app
@Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { //....... }
在onApplicationEvent中去處理
這樣就大功告成了ide