SpringMVC何時載入的controller裡的mapping方法
疑問
其實我一直有一個疑問,因為我在跟自己寫的controller的生命週期原始碼的時候,沒有發現解析mapping的程式碼,然後我就在想,什麼時候解析並載入的mapping呢???
結果是一個新的類 RequestMappingHandlerMapping ,驚呆了,我的小夥伴
demo下載
https://github.com/cbeann/Demooo/tree/master/springmvc-demo-app
原始碼跟進
我就從RequestMappingHandlerMapping的生命週期的afterPropertiesSet()方法講起
//RequestMappingHandlerMapping
@Override
public void afterPropertiesSet() {
this.config = new RequestMappingInfo.BuilderConfiguration();
this.config.setUrlPathHelper(getUrlPathHelper());
this.config.setPathMatcher(getPathMatcher());
this.config.setSuffixPatternMatch(this.useSuffixPatternMatch);
this.config.setTrailingSlashMatch(this.useTrailingSlashMatch);
this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
this.config.setContentNegotiationManager(getContentNegotiationManager());
//呼叫父類的方法,跟進去--->
super.afterPropertiesSet();
}
//AbstractHandlerMethodMapping
protected void initHandlerMethods() {
//遍歷所有的類
for (String beanName : getCandidateBeanNames()) {
if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
//當beanName為自定義的helloController時跟進去--->
processCandidateBean(beanName);
}
}
handlerMethodsInitialized(getHandlerMethods());
}
//AbstractHandlerMethodMapping
protected void processCandidateBean(String beanName) {
Class<?> beanType = null;
try {
beanType = obtainApplicationContext().getType(beanName);
}
catch (Throwable ex) {
// An unresolvable bean type, probably from a lazy bean - let's ignore it.
if (logger.isTraceEnabled()) {
logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
}
}
//如果此類上有Controller或者RequestMapping,則返回真
if (beanType != null && isHandler(beanType)) {
//跟進去--->
detectHandlerMethods(beanName);
}
AbstractHandlerMethodMapping
protected void detectHandlerMethods(Object handler) {
Class<?> handlerType = (handler instanceof String ?
obtainApplicationContext().getType((String) handler) : handler.getClass());
if (handlerType != null) {
Class<?> userType = ClassUtils.getUserClass(handlerType);
//解析類並且獲取所有帶有requestMapping的方法
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
(MethodIntrospector.MetadataLookup<T>) method -> {
try {
return getMappingForMethod(method, userType);
}
catch (Throwable ex) {
throw new IllegalStateException("Invalid mapping on handler class [" +
userType.getName() + "]: " + method, ex);
}
});
if (logger.isTraceEnabled()) {
logger.trace(formatMappings(userType, methods));
}
//此時將method和mapping都注入到某個地方(map)中,後面就可以直接使用了
methods.forEach((method, mapping) -> {
Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
registerHandlerMethod(handler, invocableMethod, mapping);
});
}
}
此時就已經明白了,並不是在自定義的controller類的生命週期內解析的mapping,而是在RequestMappingHandlerMapping 生命週期的afterPropertiesSet()方法中獲取所有的controller類並解析
相關文章
- 【3】SpringMVC的ControllerSpringMVCController
- 聊聊springmvc中controller的方法的引數註解SpringMVCController
- Controller和View物件的載入ControllerView物件
- SpringMvc的Controller singleton synchronizedSpringMVCControllersynchronized
- 實現springmvc 的controller級別的跳轉的方法 return "redirect:/news/to";SpringMVCController
- springMVC中controller的返回值SpringMVCController
- SAP Fiori Elements controller extension 的載入原理Controller
- $(function(){}裡面方法不載入問題Function
- 拙見--springMVC的controller接受的請求引數SpringMVCController
- SpringMVC使用註解完成 URL 和 Controller & 方法之間的對映細節SpringMVCController
- SAPCRMOneorder裡userstatus和systemstatus的mapping邏輯APP
- 類的載入時機
- SpringMVC的controller提供了PUT和DELETE的請求方式SpringMVCControllerdelete
- 反射-類的載入概述和載入時機反射
- 何時入場才是好時機
- SpringMVC【開發Controller】詳解SpringMVCController
- SpringMVC 解析(三) Controller 註解SpringMVCController
- SpringMVC - controller中獲取sessionSpringMVCControllerSession
- Oracle的Nologging何時生效 與 批次insert載入資料效率.Oracle
- SpringMVC的下載功能SpringMVC
- SpringMVC 基於註解的Controller @RequestMapping @RequestParamSpringMVCControllerAPP
- Spring 原始碼解析一:SpringMVC 的載入機制原始碼SpringMVC
- Laravel Eloquent中的 懶載入VS即時載入Laravel
- js 進入頁面載入的方法JS
- Oracle的Nologging何時生效 與 批次insert載入資料速度(zt)Oracle
- Oracle的Nologging何時生效 與 批量insert載入資料速度(zt)Oracle
- 懶載入簡單的方法
- 動態載入JS的方法JS
- 何時使用自定義HTTP 方法HTTP
- npm 設定同時從多個包源載入包的方法NPM
- Android執行時ART載入類和方法的過程分析Android
- 在jboss啟動時載入class方法(定時操作) (轉)
- SpringMVC原始碼之Handler註冊、獲取以及請求controller中方法SpringMVC原始碼Controller
- express框架路由配置及controller自動載入Express框架路由Controller
- js資料載入完成的方法JS
- aix 6.0 載入磁帶的方法AI
- SpringMVC之HanderMapping-3種訪問Controller的方式SpringMVCAPPController
- SpringMvc自動裝配@Controller無效SpringMVCController