当前位置:网站首页>@How to resolve controller and requestmapping

@How to resolve controller and requestmapping

2022-06-11 12:58:00 yingyinqixian

1, annotation

Click in @RestController You can see it in it @Controller and @RsponseBody annotation , How does this annotation identify our request path , It is mainly divided into two steps , The first step is to spring initialization , After the project is started and initialized , The second step is to process user requests , adopt springmvc Medium dispatchservlet call org.springframework.web.servlet.handler.AbstractHandlerMapping#getHandler Method returns the execution chain HandlerExecutionChain, Get into getHandler Method will find that it will call org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#getHandlerInternal Method

2, initialization

2.1,AbstractHandlerMethodMapping

First look at abstract classes AbstractHandlerMethodMapping, Realized InitializingBean Interface rewrites afterPropertiesSet,InitializingBean There is an important way afterPropertiesSet

2.2,InitializingBean

When a class implements this interface ,Spring After starting , initialization Bean when , If so Bean Realization InitialzingBean Interface , Automatically called afterPropertiesSet() Method , Complete some user-defined initialization operations , stay BeanFactory After initializing this bean, And the bean Is called once after the parameters of are successfully injected afterPropertiesSet() Method ,InitializingBean There is a function similar to XML Configuration in profile init-method,afterPropertiesSet() stay init-method Previous call , see AbstractAutowireCapableBeanFactory Class invokeInitMethods Method . Similar interfaces include SmartInitializingSingleton

1,SmartInitializingSingleton Only for single cases bean,InitializingBean No requirements . Can not be used for lazy loading bean.

2,SmartInitializingSingleton In all cases Bean Are called after initialization ,InitializingBean Is each bean After the initialization is completed, the .

2.3,detectHandlerMethods

Click in AbstractHandlerMethodMapping Medium detectHandlerMethods Method , With UserController For example

  Get into detectHandlerMethods The method

Judge handler Whether to belong to string type , If it is obtained from the context handler Of class, If not obtained directly class

Judge whether it includes cglib Bytecode separator , If you include , Get the... Of the bytecode

Super class bytecode and the class bytecode is not Object And back to

MethodIntrospector.selectMethods Method transfer 2 Parameters , The first parameter bytecode , The second parameter MetadataLookUp It's a function interface ,@FunctionalInterface Decorate similar interfaces Consumer and Function,java8 Many such function interfaces are provided , Of course, you can also customize the implementation ,inspect Methods in selectMethods Execute... On call , adopt this.getClass().isAnonymousClass() You can determine whether it is an anonymous inner class , Anonymous classes that implement interfaces , Grammar format :new < Parent class or interface > < class >, The advantage here is code reuse ,MethodIntrospector Is an abstract class that provides a common set of implementations , Subclass inheritance can use this method in general , Different implementations are implemented in subclasses , Similar functions are implemented by abstract method subclasses 、 Function interface callback, etc , This scenario belongs to function callback ; Get into MethodIntrospector class selectMethods Method , return map

Back to detectHandlerMethods Methods registerHandlerMethod Interface method , Get into registerHandlerMethod Methods the internal

 

 

3, request

原网站

版权声明
本文为[yingyinqixian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111224021070.html