@Bean
@RefreshScope
public Filter myFilter(AutowireCapableBeanFactory beanFactory) {
return new GenericFilterBean() {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, response);
}
};
}
will cause the following exceptions:
java.lang.NullPointerException: null
at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:176) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:109) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4561) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5204) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393) [tomcat-embed-core-8.5.4.jar:8.5.4]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_31]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_31]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_31]
Because GenericFilterBean will be created as a CGLIB proxy and it's 'logger' instance will be null.
public final void init(FilterConfig filterConfig) throws ServletException {
Assert.notNull(filterConfig, "FilterConfig must not be null");
if (logger.isDebugEnabled()) {
logger.debug("Initializing filter '" + filterConfig.getFilterName() + "'");
}
Comment From: dsyer
That's weird isn't it, and easy to reproduce? It must be to do with the fact that it's a Filter I think (so it is initialized very early), because otherwise I see no reason for the logger to be null.
Of course there's no terribly good reason for a Filter to be in @RefreshScope and I expect you can always refactor your code to put the scope annotation on something else and inject it into the filter.
Comment From: wilkinsona
There's nothing in Spring Boot involved in the apparently problematic proxying of GenericFilterBean . @dantesun If you'd like to pursue this (and as @dsyer explain above, we can see no reason for a Filter to be in @RefreshScope), please raise a Spring Framework issue.
Comment From: bartboersma
Same issue when applying an io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker annotation around the doFilterInternal method.
Comment From: swethavemuri
I had the same issue with a custom RequestContextFilter implementation and using de.codecentric chaos-monkey-spring-boot dependency and @RefreshScope. I fixed it by adding the below @Bean in the config class annotated with @RefreshScope.
@Order(Ordered.HIGHEST_PRECEDENCE)
@Bean
public FilterRegistrationBean contextFilterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean<>();
CustomFilter contextFilter = new CustomFilter();
registrationBean.setFilter(contextFilter);
registrationBean.setOrder(1);
return registrationBean;
}
Comment From: angelsinistanbul
I had the same issue with a custom
RequestContextFilterimplementation and usingde.codecentric chaos-monkey-spring-bootdependency and@RefreshScope. I fixed it by adding the below@Beanin the config class annotated with@RefreshScope.
java @Order(Ordered.HIGHEST_PRECEDENCE) @Bean public FilterRegistrationBean contextFilterRegistrationBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); CustomFilter contextFilter = new CustomFilter(); registrationBean.setFilter(contextFilter); registrationBean.setOrder(1); return registrationBean; }
Hello,
Could you share it whole class? I am having same issue and applied your solutiom. Not facing error but bean not refreshing.. I