zoukankan      html  css  js  c++  java
  • Spring 加载Controller逻辑的源码笔记

    org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#initHandlerMethods 进行加载Controller:

    protected void initHandlerMethods() {
    		if (logger.isDebugEnabled()) {
    			logger.debug("Looking for request mappings in application context: " + getApplicationContext());
    		}
    		String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
    				BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
    				getApplicationContext().getBeanNamesForType(Object.class));
    
    		for (String beanName : beanNames) {
    			if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
    				Class<?> beanType = null;
    				try {
    					beanType = getApplicationContext().getType(beanName);
    				}
    				catch (Throwable ex) {
    					// An unresolvable bean type, probably from a lazy bean - let's ignore it.
    					if (logger.isDebugEnabled()) {
    						logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
    					}
    				}
    				if (beanType != null && isHandler(beanType)) {  //isHandler 是重点方法,判断是否是Controller
    detectHandlerMethods(beanName); } } } handlerMethodsInitialized(getHandlerMethods()); }

     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#isHandler 源码如下:

    	/**
    	 * {@inheritDoc}
    	 * Expects a handler to have a type-level @{@link Controller} annotation.
    	 */
    	@Override
    	protected boolean isHandler(Class<?> beanType) {
    		return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
    				AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
    	}  

     

    isHandler 进行判断是否是有Controller或者RequestMapping注解,如果有的话就开始进行映射Controller的方法。
    具体映射逻辑在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#detectHandlerMethods中。
  • 相关阅读:
    web字体
    解决input之间的空隙
    CSS基础:text-overflow:ellipsis溢出文本
    css3控制内容的可选择性
    设置dt height 保证dd在同一行
    extjs Ext.Ajax.request 同步和异步
    jquery 同步和异步请求
    freemarker 基础
    freemarker简单使用示例
    【数据结构】线性表顺序结构的操作---C/C++语言
  • 原文地址:https://www.cnblogs.com/leodaxin/p/8387599.html
Copyright © 2011-2022 走看看