转自:
AbstractHandlerMethodMapping类的功能简介说明
下文笔者将讲述AbstractHandlerMethodMapping类的功能简介,如下所示:
AbstractHandlerMethodMapping的功能:
当bean被注入到容器后会执行一系列的初始化过程
用于注解@Controller,@RequestMapping来定义controller。
AbstractHandlerMethodMapping源码
//抽象方法,并实现了initializingBean接口,其实主要的注册操作则是通过afterPropertiesSet()接口方法来调用的
public abstract class AbstractHandlerMethodMappingextends AbstractHandlerMapping implements InitializingBean{}
//容器启动时会运行此方法,完成handlerMethod的注册操作
@Override
public void afterPropertiesSet() {
initHandlerMethods();
}
//handlerMethod的注册操作
protected void initHandlerMethods() {
if (logger.isDebugEnabled()) {
logger.debug("Looking for request mappings in application context: " + getApplicationContext());
}
//从springMVC容器中获取所有的beanName
String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
getApplicationContext().getBeanNamesForType(Object.class)); //注册从容器中获取的beanName
for (String name : beanNames) {
if (!name.startsWith(SCOPED_TARGET_NAME_PREFIX) && isHandler(getApplicationContext().getType(name))) {
detectHandlerMethods(name);
}
}
handlerMethodsInitialized(getHandlerMethods());
}
//根据beanName进行一系列的注册,最终实现是在registerHandlerMethod
protected void detectHandlerMethods(final Object handler) {
//获取bean实例
Class handlerType =
(handler instanceof String ? getApplicationContext().getType((String) handler) : handler.getClass());
// Avoid repeated calls to getMappingForMethod which would rebuild RequestMappingInfo instances
final Map mappings = new IdentityHashMap();
final Class userType = ClassUtils.getUserClass(handlerType);
Setmethods = HandlerMethodSelector.selectMethods(userType, new MethodFilter() {
@Override
public boolean matches(Method method) {
//创建RequestMappingInfo
T mapping = getMappingForMethod(method, userType);
if (mapping != null) {
mappings.put(method, mapping);
return true;
}
else {
return false;
}
}
});
for (Method method : methods) {
registerHandlerMethod(handler, method, mappings.get(method));
}
}
//注册beanName和method及RequestMappingInfo之间的关系,RequestMappingInfo会保存url信息
@Deprecated
protected void registerHandlerMethod(Object handler, Method method, T mapping) {
this.mappingRegistry.register(mapping, handler, method);
}
Original: https://blog.csdn.net/qq_25073223/article/details/127815791
Author: qq_25073223
Title: AbstractHandlerMethodMapping类的功能简介说明
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/655123/
转载文章受原作者版权保护。转载请注明原作者出处!