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類並解析
相關文章
- SpringMvc的Controller singleton synchronizedSpringMVCControllersynchronized
- springMVC中controller的返回值SpringMVCController
- SAP Fiori Elements controller extension 的載入原理Controller
- 拙見--springMVC的controller接受的請求引數SpringMVCController
- SpringMVC【開發Controller】詳解SpringMVCController
- SpringMVC 解析(三) Controller 註解SpringMVCController
- SAPCRMOneorder裡userstatus和systemstatus的mapping邏輯APP
- Spring 原始碼解析一:SpringMVC 的載入機制原始碼SpringMVC
- SpringMVC常用註解@Controller,@Service,@repository,@ComponentSpringMVCController
- SpringMVC的下載功能SpringMVC
- Springmvc mvc:exclude-mapping不攔截 無效SpringMVCAPP
- SpringMVC原始碼之Handler註冊、獲取以及請求controller中方法SpringMVC原始碼Controller
- $(function(){}裡面方法不載入問題Function
- [elixir! #0082] application controller 應用是如何被載入和啟動的APPController
- 類的載入時機
- 說說SpringMVC從http流到Controller介面引數的轉換過程SpringMVCHTTPController
- js 進入頁面載入的方法JS
- Laravel Eloquent中的 懶載入VS即時載入Laravel
- 何時入場才是好時機
- SpringMVC-RequestContextHolder的使用 -- 轉載SpringMVCContext
- ElasticSearch 中的 MappingElasticsearchAPP
- npm 設定同時從多個包源載入包的方法NPM
- TensorFlow 載入多個模型的方法模型
- flutter pub get 的時候:A dependency specification must be a string or a mapping.FlutterAPP
- SAP CRM One order裡user status和system status的mapping邏輯APP
- 請列舉出多種減少頁面載入時間的方法
- 一種基於KVO的頁面載入,渲染耗時監控方法
- VS載入lib庫的三種方法
- SpringMVC 入門SpringMVC
- SpringMVC入門SpringMVC
- SpringMVC(三)處理器方法形參繫結(繫結陣列、集合)、異常處理器、controller方法返回值SpringMVC陣列Controller
- SpringMVC的亂碼與時間轉換SpringMVC
- 類載入-載入時機學習
- SpringBoot載入子模組配置檔案的方法Spring Boot
- Vue路由元件按需載入的幾種方法Vue路由元件
- Sora 時代的 AI 影片生成何去何從?SoraAI
- 使用者登入時的環境載入順序
- app直播原始碼,等待時的載入動畫(交錯載入動畫)APP原始碼動畫