SpringMVC 笔记篇

1.1 执行流程

1.1.7 RequestMappingHandlerAdapter.afterPropertiesSet()

除了HandlerMapping之外,其他的有没有自己的初始化逻辑呢,也是有的,我们拿HandlerAdapter中的RequestMappingHandlerAdapter来举例,那么首先我们找到RequestMappingHandlerAdapter

进去之后我们能看到afterPropertiesSet()方法,下面有一个initControllerAdviceCache()方法,跟进这个方法

 这里重点是findAnnotatedBeans()方法,跟进一下这个方法

结合上面的几张截图,这套流程首先通过initControllerAdviceCache()方法,去扫描ControllerAdvice,首先会从我们的Spring容器里面通过findAnnotatedBeans()方法,去找那些加了ControllerAdvice注解的Bean (@ControllerAdvice)

import org. springframework. stereotype. Component;
import org. springframework. web. bind. annotation. ControllerAdvice;

@Component
@ControllerAdvice
public class XXXControllerAdvice {
}

当我们定义了这样一个Bean,在我们加上@ControllerAdvice注解之后,首先通过findAnnotatedBeans()方法把我们加了@ControllerAdvice注解的Bean对象给找出来

然后一个个的去遍历,遍历Bean类型

然后基于Bean的类型去找方法(找我们定义的Bean对象里面的方法,也就是XXXControllerAdvice 里面的方法) 那怎么去找呢?

比如说首先去找加了ModelAttribute注解但是没有加RequestMapping注解的方法,举个例子

import org. springframework. stereotype. Component;
import org. springframework. web. bind. annotation. ControllerAdvice;

@Component
@ControllerAdvice
public class XXXControllerAdvice {
   
   @ModelAttribute
   public void test(){


   }

}

像这样初始化的时候就会找到里面的test()方法,同样的,也可以根据源码来看,尝试去找加了其他注解的方法,比如@InitBinder注解,这里不在一一列举

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

康康峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值