Springboot @WebFilter无法注入其他Bean的实例问题
示例问题代码:
@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内容请搜索无名以前的文章或继续浏览下面的相关文章希望大家以后多多支持无名!
同类资源
- Java基于springboot教务管理系统
Java基于springboot教务管理系统本文件感兴趣的可以参考一下,帮助学校管理教务系统,用一个帐号解决学校教务...
- springBootNoMaven实现定时任务功能
springBootNoMaven本文件感兴趣的可以参考一下,不使用maven环境,创建springboot项目,实现了定时任务功能简单...
- SpringBoot健身房管理系统
SpringBoot健身房管理系统绿色版,springboot+MySQL+thymeleaf。...
- 基于MVC用NETBEAN做的商城购物系统
基于MVC的商城购物系统例子,这个虽然有的没有看懂,但是看懂的还是很好的。...
- 使用springboot2.x整合shiro包含sql数据库
使用springboot2.x整合shiro包含sql数据库例子源代码。...