当前位置:网站首页>@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,请求
边栏推荐
- openharmony标准系统之app手动签名
- oracle数据库中 instr函数的用法
- After Oracle deletes a user, it can still use the user to log in
- How can physical stores break through operational difficulties? Take a look at the store operation guide of this physical store applet
- Jerry's aicmd command [chapter]
- 联想小新520怎么样?对比当贝D3X哪款更值得买?
- Flash framework web development video notes
- What is QoS? (quality of service)
- How does Wireshark modify the display format of packet capturing time and date?
- Evolution of e-commerce development
猜你喜欢

2、CompletableFuture

知麻Z1投影仪真的好用吗?实际效果怎么样?

Oracle DatabaseLink cross database connection

General O & M structure diagram

App manual signature of openharmony standard system

秒杀中的验证码安全机制

What are the advantages of comprehensive venues?

羽毛球馆有哪些创收方式

科海融生&正航,以信息化驱动管理升级,携手共迎数智未来

How does the beauty salon management system solve the three problems of store operation?
随机推荐
How does Wireshark modify the display format of packet capturing time and date?
C# System.Guid.NewGuid() 格式化
1. Thread Basics
Flick grouping sets multidimensional aggregation and setting table state expiration time
Oracle DatabaseLink cross database connection
Stone technology: R & D strength and excellent quality help upgrade the sweeping robot industry
罗景:连接效率优化实践
Wireshark packet capturing and debugging RTSP
秒杀整体的技术难点
1、线程基础知识
电商发展的演变
Where is it safer to open an account for soda ash futures? How much is the margin for soda ash futures?
Splunk certificate expired, making kV store unable to start
Problems encountered recently when using nodejs Pinyin package
12. AQS of abstractqueuedsynchronizer
Flash framework web development video notes
8. 18 arhat enhancements for atomic operations
node结合art-template 模板引擎 创建模板文件
Redis data type Daily use Scenarios
综合场馆的优势有哪些?