Python 官方文档:入门教程 => 点击学习
示例问题代码: @WEBFilter(filterName = "authorizeFilter", urlPatterns = {"*.htm", "*.html"}, asy
示例问题代码:
@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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Springboot @WebFilter无法注入其他Bean的示例问题
本文链接: https://lsjlt.com/news/135357.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0