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中。
  • 相关阅读:
    python模块搜索路径
    Python数据结构
    Python文件类型
    Python循环语句
    Python条件语句
    python配置文件操作——configparser模块
    python 加密方式(MD5&sha&hashlib)
    python MySQL 获取全部数据库(DATABASE)名、表(TABLE)名
    python sqlite3查看数据库所有表(table)
    027.MFC_映射消息
  • 原文地址:https://www.cnblogs.com/leodaxin/p/8387599.html
Copyright © 2011-2022 走看看