学无先后,达者为师

网站首页 编程语言 正文

解决SpringBoot拦截器中无法依赖注入bean

作者:为了我的架构师 更新时间: 2022-04-17 编程语言

SpringBoot的拦截器是在Bean实例化之前就执行了,

标题只需要在拦截器配置类里面将需要依赖注入的拦截器实例化就行了:

image-20211010201817375

在HasAuth中:RedisTemplate就能注入进来了

image-20211010201917984

标题另一种方式:在拦截器中手动从bean工厂拿

BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
factory.getBean("xxxx");

原文链接:https://blog.csdn.net/qq_42682745/article/details/120691641

栏目分类
最近更新