Question

In app we have functionality of update configuration on fly. It overrides application properties and call:

  ((ConfigurableApplicationContext)applicationContext).refresh();

We are also using DelegatingFilterProxy to register Spring bean as a filter.

@Override
protected Filter[] getServletFilters() {
    DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy("myFilter");
    delegatingFilterProxy.setContextAttribute(FrameworkServlet.SERVLET_CONTEXT_PREFIX + "dispatcher");

    return new Filter[] { delegatingFilterProxy };
}

When I reload application.properties I see that Spring reccreate all beans but DelegatingFilterProxy still using first version of filter bean (so all requests are filtered by stale filter with stale autowired beans on it)

Do you know why it happens how to get rid of this?

Was it helpful?

Solution

I can tell you why it happens.

I'm assuming you're registering your DelegatingFilterProxy in a WebApplicationInitializer subclass, maybe an AbstractDispatcherServletInitializer subclass.

Regardless, this class is completely independent of actions to the ApplicationContext. The Servlet container scans your classpath and finds Spring's SpringServletContainerInitializer which finds your WebApplicationInitializer implementation and runs it. The DelegatingFilterProxy is then registered specifying a bean name.

When the first request comes in to your server, the DelegatingFilterProxy will attempt to find its delegate in the ApplicationContext and set it to its delegate field. It does this only if its current delegate is null. So a refresh of the context won't affect it.

There are possible solutions here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top