广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring WebMVC初始化Controller流程详解
  • 692
分享到

Spring WebMVC初始化Controller流程详解

2024-04-02 19:04:59 692人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录springWEBmvc初始化Controller流程获取容器初始化的所有beanName(父子容器概念)获取所有声明为Controller类的beanName开始处理这种类型的

Spring WebMVC初始化Controller流程

此篇文章开始之前先向大家介绍一个接口 InitializingBean

这个接口的作用如果了解spring生命周期的应该知道 ,这个接口的作用就是在bean初始化之后会执行的init方法

public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}

当然能实现这种方式的方法spring还介绍了关于注解的@PostConstruct 和xml 的 init-method = "" 的两种方式。但是springMVC使用的是接口的方式。

这里要介绍的初始化Controller是指填充完HandlerMapping map<String,Method>即代表初始化Controller流程

我们再提一点小知识。声明一个controller类有哪些方式。(现在应该没有人用第二/三种方式吧)

  • 1.使用注解@Controller 和 请求路径@RequestMapping
  • 2.实现 Controller 接口 并将该类交给spring容器管理beanName为请求路径
  • 3.实现 HttpRequestHandler 接口并将该类交给spring容器管理beanName为请求路径

那么我们的map填充就从实现了InitializingBean 接口 的afterPropertiesSet这个方法开始。

源码中是这个类 AbstractHandlerMethodMapping(下面的代码有删减)

public void afterPropertiesSet() {
    this.initHandlerMethods();
}
protected void initHandlerMethods() {
    String[] var1 = this.getCandidateBeanNames();//1.获取容器初始化的所有beanName
    int var2 = var1.length;
    for(int var3 = 0; var3 < var2; ++var3) {
        String beanName = var1[var3];
        if (!beanName.startsWith("scopedTarget.")) {
            this.processCandidateBean(beanName);//2.获取所有声明为Controller类的beanName
        }
    }
    this.handlerMethodsInitialized(this.getHandlerMethods());
}

获取容器初始化的所有beanName(父子容器概念)

protected String[] getCandidateBeanNames() {
    return this.detectHandlerMethodsInAncestorContexts ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.obtainApplicationContext(), Object.class) : this.obtainApplicationContext().getBeanNamesForType(Object.class);
}

获取所有声明为Controller类的beanName

protected void processCandidateBean(String beanName) {
    Class beanType = null;
    try {
        beanType = this.obtainApplicationContext().getType(beanName);//获取bean的类型
    } catch (Throwable var4) {
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("Could not resolve type for bean '" + beanName + "'", var4);
        }
    }
    if (beanType != null && this.isHandler(beanType)) {//获取所有声明为Controller类的beanName
        this.detectHandlerMethods(beanName);//1.开始处理这种类型的beanName
    }
}

开始处理这种类型的beanName

protected void detectHandlerMethods(Object handler) {
    Class<?> handlerType = handler instanceof String ? this.obtainApplicationContext().getType((String)handler) : handler.getClass();
    if (handlerType != null) {
        Class<?> userType = ClassUtils.getUserClass(handlerType);
        Map<Method, T> methods = MethodIntrospector.selectMethods(userType, (method) -> {
            try {
                return this.getMappingFORMethod(method, userType);//1.获取到类类型下所有的方法
            } catch (Throwable var4) {
                throw new IllegalStateException("Invalid mapping on handler class [" + userType.getName() + "]: " + method, var4);
            }
        });
        if (this.logger.isTraceEnabled()) {
            this.logger.trace(this.formatMappings(userType, methods));
        }
        methods.forEach((method, mapping) -> {
            Method invocableMethod = aopUtils.selectInvocableMethod(method, userType);
            this.reGISterHandlerMethod(handler, invocableMethod, mapping);//注册并填充map
        });
    }
}

获取到类类型下所有的方法和注册并填充map

第一个 MultiValueMap<String, T> urlLookup = new LinkedMultiValueMap();

urlLookup.add(url, mapping);
eg:url = '/test/test.do' 

mapping是一个RequestMappingInfo 对象 RequestMappingInfo.patternsCondition = T --> /test/test.do

第二个 Map<T, AbstractHandlerMethodMapping.MappingRegistration<T>> registry = new HashMap();

eg:key = 'url'  value = 'method'

而我们的第二种和第三种的方式基本没有用了,因为会出现类爆炸,就像原始的servlet一样每一个方法都需要写一个类。

这两种方式是通过beanName为路径来实例化对象并执行通过该对象来执行里面的方法的。

源码中这两种map的填充方式是在bean的生命周期中通过实现beanFactory的applyBeanPostProcessorsBeforeInitialization方法来填充的。 

@Controller 类中初始化问题

在Controller类中常常遇到有些参数需要初始化,甚至有些只允许初始化一次,而Controller类不像servelet类可以调用init()函数进行初始化,这里想到的办法是设置标记值,让初始化部分只调用一次。

第一种方法

设置isStart值。

private static Boolean isStart = false;
if(!isStart){
  //进行初始化
  isStart=true;
}

第二种方法

使用注释@PostConstruct,该注释的类会在类初始化时进行调用。

@PostConstruct
private void init(){
//进行初始化
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Spring WebMVC初始化Controller流程详解

本文链接: https://www.lsjlt.com/news/140001.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作