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); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。