Springboot @WebFilter无法注入其他Bean的示例问题
程序员文章站
2022-03-04 15:01:06
示例问题代码:@webfilter(filtername = "authorizefilter", urlpatterns = {"*.htm", "*.html"}, asyncsupported...
示例问题代码:
@webfilter(filtername = "authorizefilter", urlpatterns = {"*.htm", "*.html"}, asyncsupported = true) public class authorizefilter implements filter { @autowired private otherbean otherbean; @override public void init(filterconfig filterconfig) throws servletexception { } @override public void destroy() { } @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { // true system.out.println(otherbean == null); } }
现象:
本地运行测试均可通过,上测试环境后运行注入bean为空
现象:使用外置tomcat可触发,本地使用内置tomcat则无此问题
解决代码
@component public class authorizefilter implements filter { @autowired private otherbean otherbean; @override public void init(filterconfig filterconfig) throws servletexception { } @override public void destroy() { } @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { // false system.out.println(otherbean == null); } }
@configuration public class webfilterconfig implements webmvcconfigurer { @autowired private authorizefilter authorizefilter; @bean("authorizefilterbean") public filterregistrationbean authorizefilterbean() { filterregistrationbean registration = new filterregistrationbean(); registration.setfilter(authorizefilter); registration.addurlpatterns(new string[]{"*.htm", "*.html"}); registration.setname("authorizefilter"); registration.setasyncsupported(true); return registration; } }
启动类加上:@servletcomponentscan({“com.hybase.site.filter”})
到此这篇关于springboot @webfilter无法注入其他bean的示例问题的文章就介绍到这了,更多相关springboot 无法注入bean内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
详解SpringBoot 多线程处理任务 无法@Autowired注入bean问题解决
-
消息队列监听器无法注入bean的问题解决
-
mybatis拦截器无法注入spring bean的问题解决
-
解决SpringBoot2多线程无法注入的问题
-
springboot2.x解决运行顺序及Bean对象注入顺序的问题
-
聊聊SpringBoot中组件无法被注入的问题
-
Springboot @WebFilter无法注入其他Bean的示例问题
-
SpringBoot之拦截器注入Bean的方法介绍(代码示例)
-
解决static静态方法无法使用@Autowired注入的mapper/bean等元的问题
-
解决SpringBoot2多线程无法注入的问题