Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@RefreshScope GenericFilterBean will fail to initialize #7151

Closed
dantesun opened this issue Oct 12, 2016 · 4 comments
Closed

@RefreshScope GenericFilterBean will fail to initialize #7151

dantesun opened this issue Oct 12, 2016 · 4 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@dantesun
Copy link

@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() + "'");
        }
@dantesun dantesun changed the title @RefreshScope GenericFilterBean will fail to initialize due to cglib proxy issues @RefreshScope GenericFilterBean will fail to initialize Oct 12, 2016
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 12, 2016
@dsyer
Copy link
Member

dsyer commented Oct 14, 2016

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.

@dsyer dsyer added for: team-attention An issue we'd like other members of the team to review and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 14, 2016
@philwebb philwebb added type: bug A general bug and removed for: team-attention An issue we'd like other members of the team to review labels Oct 26, 2016
@wilkinsona
Copy link
Member

wilkinsona commented Nov 2, 2016

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.

@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid and removed type: bug A general bug labels Nov 2, 2016
@bartboersma
Copy link

bartboersma commented May 14, 2020

Same issue when applying an io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker annotation around the doFilterInternal method.

@swethavemuri
Copy link

swethavemuri commented Dec 15, 2020

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

7 participants