Springboot|Springboot @WebFilter无法注入其他Bean的示例问题
示例问题代码:
@WebFilter(filterName = "authorizeFilter", urlPatterns = {"*.htm", "*.html"}, asyncSupported = true)public class AuthorizeFilter implements Filter { @Autowired private OtherBean otherBean; @Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void destroy() {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {// trueSystem.out.println(otherBean == null); }}
现象:
本地运行测试均可通过,上测试环境后运行注入bean为空解决代码
现象:使用外置tomcat可触发,本地使用内置tomcat则无此问题
@Componentpublic class AuthorizeFilter implements Filter { @Autowired private OtherBean otherBean; @Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void destroy() {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {// falseSystem.out.println(otherBean == null); }}
@Configurationpublic class WebFilterConfig implements WebMvcConfigurer {@Autowiredprivate 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|Springboot @WebFilter无法注入其他Bean的示例问题】到此这篇关于Springboot @WebFilter无法注入其他Bean的示例问题的文章就介绍到这了,更多相关Springboot 无法注入Bean内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- Android7.0|Android7.0 第三方应用无法访问私有库
- 无论你是谁,都有两件事,无法掌控
- 没有思考时
- springboot使用redis缓存
- springboot整合数据库连接池-->druid
- 2021.4.8日《我们为什么无法摆脱慢性疾病》常斌
- 插件化无法获取或找到.so文件