当前位置:网站首页>@Controller和RequestMapping如何解析的
@Controller和RequestMapping如何解析的
2022-06-11 12:24:00 【yingyinqixian】
1,注解
点进@RestController里面可以看到@Controller和@RsponseBody注解,这个注解是如何识别我们的请求路径的,主要分为两步,第一步是spring初始化,项目启动初始化以后,第二步就是处理用户请求,通过springmvc中的dispatchservlet调用org.springframework.web.servlet.handler.AbstractHandlerMapping#getHandler方法返回执行链HandlerExecutionChain,进入getHandler方法会发现会调用org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#getHandlerInternal方法

2,初始化
2.1,AbstractHandlerMethodMapping
首先看抽象类AbstractHandlerMethodMapping,实现了InitializingBean接口重写了afterPropertiesSet,InitializingBean有个重要方法afterPropertiesSet


2.2,InitializingBean
当一个类实现这个接口之后,Spring启动后,初始化Bean时,若该Bean实现InitialzingBean接口,会自动调用afterPropertiesSet()方法,完成一些用户自定义的初始化操作,在BeanFactory初始化完这个bean,并且把bean的参数都注入成功后调用一次afterPropertiesSet()方法,InitializingBean有个功能类似在XML配置文件中配置init-method,afterPropertiesSet()在init-method之前调用,查看AbstractAutowireCapableBeanFactory类中invokeInitMethods方法。类似接口还有SmartInitializingSingleton

1,SmartInitializingSingleton只作用于单例bean,InitializingBean无要求。都不能用于懒加载的bean。
2,SmartInitializingSingleton是在所有单例Bean都初始化完成后调用的,InitializingBean是每个bean初始化完成后就会调用。

2.3,detectHandlerMethods
点进AbstractHandlerMethodMapping中的detectHandlerMethods方法,以UserController为例

进入detectHandlerMethods该方法
判断handler是否属于string类型,如果是从上下文获取handler的class,如果不是直接获取class

判断是否包含cglib字节码分隔符,如果包含,获取该字节码的
超类字节码并且该类字节码不是Object并返回

MethodIntrospector.selectMethods方法传递2个参数,第一个参数字节码,第二参数MetadataLookUp是函数接口,@FunctionalInterface修饰类似接口还有Consumer和Function,java8提供了许多这样的函数接口,当然也可以自定义实现,inspect方法会在selectMethods调用时执行,通过this.getClass().isAnonymousClass()可以判断出是否是匿名内部类,实现接口的匿名类,语法格式:new <父类或接口> <类>,这里的好处是代码复用,MethodIntrospector是一个抽象类提供一套通用实现,子类继承可通用此方法,不同的实现在子类实现,类似功能还有抽象方法子类实现、函数接口回调等,此种场景属于函数回调;进入MethodIntrospector类selectMethods方法,返回map

再回到detectHandlerMethods方法中的registerHandlerMethod接口方法,进入registerHandlerMethod方法内部


3,请求
边栏推荐
- How to optimize SEO on the mobile terminal? Do you need to optimize GoogleSEO on the mobile terminal for a separate m domain name?
- How Oracle exports data to CSV (Excel) files
- Redis数据类型日常使用场景
- 2、CompletableFuture
- Jerry's CMD_ SET_ BT_ Name command format [chapter]
- mysql的主从复制
- Is Zhima Z1 projector really easy to use? How about the actual effect?
- What are the profit sources of mother and baby stores?
- Application of "knowing things by learning" behavior time series modeling in the identification of social drainage black production
- Jerry's AI CMD_ SET_ BLE_ Name [chapter]
猜你喜欢

Redis數據類型日常使用場景

9. Parler de threadlocal

1、线程基础知识

9、聊聊ThreadLocal

How about Lenovo Xiaoxin 520? Which is more worth buying than dangbei D3x?

Troubleshoot Splunk kvstore "starting"

Flick controls window behavior (trigger, remover, allow delay, put late data into side output stream)

How can physical stores break through operational difficulties? Take a look at the store operation guide of this physical store applet

母婴店的利润来源有哪些?

Redis data type Daily use Scenarios
随机推荐
非标自动化设备制造企业,如何借助ERP系统实现快速精准报价?
2020.10.27 北京阿里大文娱一面总结
Seckill multi-level cache ----- product details page
How can I use a domain name to access a website?
C event bus
How does data age in Splunk?
2. Completablefuture
Application of "knowing things by learning" behavior time series modeling in the identification of social drainage black production
记录一次jvm GC过程
In the list of 618 projector hedging brands in 2022, dangbei projection ranked top 1 in the hedging rate of idle fish
Flink window table valued function
How can non-standard automation equipment manufacturing enterprises achieve rapid and accurate quotation with the help of ERP system?
8. 18 arhat enhancements for atomic operations
Schéma de dessin utilisé par les Pads
Installation and use of saltstack
Format of Jerrys at protocol package [chapter]
Is Zhima Z1 projector really easy to use? How about the actual effect?
7、CAS
This is our golden age
4. Locksupport and thread interruption