Springboot @WebFilter無(wú)法注入其他Bean的示例問(wèn)題
示例問(wèn)題代碼:
@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);
}
}
現(xiàn)象:
本地運(yùn)行測(cè)試均可通過(guò),上測(cè)試環(huán)境后運(yùn)行注入bean為空
現(xiàn)象:使用外置tomcat可觸發(fā),本地使用內(nèi)置tomcat則無(wú)此問(wèn)題
解決代碼
@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;
}
}
啟動(dòng)類加上:@ServletComponentScan({“com.hybase.site.filter”})
到此這篇關(guān)于Springboot @WebFilter無(wú)法注入其他Bean的示例問(wèn)題的文章就介紹到這了,更多相關(guān)Springboot 無(wú)法注入Bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+Echarts實(shí)現(xiàn)請(qǐng)求后臺(tái)數(shù)據(jù)顯示餅狀圖
這篇文章主要介紹了SpringBoot+Echarts實(shí)現(xiàn)請(qǐng)求后臺(tái)數(shù)據(jù)顯示餅狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Java設(shè)計(jì)模式之備忘錄模式(Memento模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之備忘錄模式(Memento模式)介紹,memento是一個(gè)保存另外一個(gè)對(duì)象內(nèi)部狀態(tài)拷貝的對(duì)象,這樣以后就可以將該對(duì)象恢復(fù)到原先保存的狀態(tài),需要的朋友可以參考下2015-03-03
解決springboot MultipartFile文件上傳遇到的問(wèn)題
本文給大家?guī)?lái)了解決springboot MultipartFile文件上傳遇到的問(wèn)題,解決方法超簡(jiǎn)單,感興趣的朋友參考下本文2018-08-08
SpringMVC基于注解方式實(shí)現(xiàn)上傳下載
本文主要介紹了SpringMVC基于注解方式實(shí)現(xiàn)上傳下載,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
配置springboot項(xiàng)目使用外部tomcat過(guò)程解析
這篇文章主要介紹了配置springboot項(xiàng)目使用外部tomcat過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
SpringBoot中的@ApiModelProperty注解作用
這篇文章主要介紹了SpringBoot中的@ApiModelProperty注解作用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01
@PathVariable注解,讓spring支持參數(shù)帶值功能的案例
這篇文章主要介紹了@PathVariable注解,讓spring支持參數(shù)帶值功能的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
SpringBoot應(yīng)用自定義logback日志詳解
默認(rèn)情況下,SpringBoot內(nèi)部使用logback作為系統(tǒng)日志實(shí)現(xiàn)的框架,將日志輸出到控制臺(tái),不會(huì)寫到日志文件。本篇文章主要講解下如何自定義logabck.xml以及對(duì)logback文件中配置做一個(gè)詳解,需要的可以參考一下2022-10-10

