• AbstractHandlerMethodMapping类的功能简介说明


    转自:

    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);  
    }  
  • 相关阅读:
    大数据关键技术:自然语言处理入门篇
    【OBS】Dropped Frames And General Connection Issues
    双向绑定与单向数据流之争,Solid会取代React吗
    垃圾分类热词获取易语言代码
    跨境电商客服回复话术
    【C++】模板初阶
    数组指针、二级指针传参
    uniapp添加极光推送
    RESR开发
    易基因科技|单细胞甲基化测序低至2500元/样
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/127815791