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中。
  • 相关阅读:
    log4j2 配置详解
    MANIFEST.MF文件详解
    assembly.xml
    firewall 和 iptables 常用命令
    Spring boot 使用 configuration 获取的属性为 null
    使用 maven-assembly-plugin 打包项目
    使用 Maven 插件将 class(字节码文件),resource(资源文件),lib(依赖的jar包)分开打包
    HttpClient 传输文件的两种方式
    IDEA中读取 resource目录下文件
    3.2、Android Studio在物理设备中运行APP
  • 原文地址:https://www.cnblogs.com/leodaxin/p/8387599.html
Copyright © 2011-2022 走看看