You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ServletContextInitializerBeans will register the "myFilter" bean twice. one is "myFilter"(by DelegatingFilterProxyRegistrationBean), the other is "scopedTarget.myFilter" due to the automatically find and add Filter type beans to ServletContext.
If there's no explicit FilterRegistrationBean declared, only one bean is registered as a filter and it's scopedTarget.myFilter. The problem appears to be that the logic in ServletContextInitializerBeans that looks for an existing registration bean doesn't find the one that's been created for the @RefreshScopeFilter so it creates one and we end up with two.
In the case where there's just myFilter (and no explicit registration bean), there are two bean definitions, one named myFilter and one named scopedTarget.myFilter. The latter matches when looking for beans of type Filter but the former does not. The former doesn't match as it's actually a ScopedProxyFactoryBean and there's insufficient type information available to match it as a Filter.
When an explicit registration bean is added it has an interesting side-effect. It results in the myFilter bean being created earlier which gives the container more to go on when it's looking for a match. It no longer sees myFilter purely as a definition for a ScopedProxyFactoryBean but can now use the singleton instance for type matching. As a result, both myFilter and scopedTarget.myFilter are registered.
When there's no explicit FilterRegistrationBean the raw scopedTarget.myFilter bean is registered rather than myFilter which is the scoped proxy. This means that refresh has no effect.
When there is an explicit FilterRegistrationBean the filter's registered twice. The faulty implicit registration described above and explicit registration. The explicit registration registers myFilter with the servlet container which is the scoped proxy so one of the two filters refreshes correctly.
diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java
index ffafe68..a831c58 100644
--- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java+++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java@@ -36,6 +36,7 @@ import javax.servlet.Servlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
@@ -226,10 +227,10 @@ public class ServletContextInitializerBeans
}
};
- String[] names = beanFactory.getBeanNamesForType(type, true, false);+ String[] names = beanFactory.getBeanNamesForType(type, true, true);
Map<String, T> map = new LinkedHashMap<String, T>();
for (String name : names) {
- if (!excludes.contains(name)) {+ if (!excludes.contains(name) && !ScopedProxyUtils.isScopedTarget(name)) {
T bean = beanFactory.getBean(name, type);
if (!excludes.contains(bean)) {
map.put(name, bean);
However, it's relying on eager init being allowed to correctly determine the type that will be produced by the ScopedProxyFactoryBean. I doubt that that's a good idea.
@wilkinsona In RefreshScope, there's another flag controlling eager-init behavior. So "beanFactory.getBeanNamesForType(type, true, true)" won't eager init the "RefreshScope" bean with "eager" set to false. Am I right?
The specifics of refresh scope are only one small part of it. Allowing eager init will initialise all lazy-init singletons and factory beans as part of the lookup and we don't want to do that.
Names in post construct: [scopedTarget.myBean]
…
Names post refresh: [scopedTarget.myBean, myBean]
I think we may need an enhancement to the container so that myBean is found in post construct which is called before the ScopedProxyFactoryBean has been instantiated. @snicoll is on the case.
Activity
philwebb commentedon Oct 12, 2016
We had a similar issue with
@MockBean(see #5724). I guess we need to filter thescopedTarget.versions.wilkinsona commentedon Oct 14, 2016
If there's no explicit
FilterRegistrationBeandeclared, only one bean is registered as a filter and it'sscopedTarget.myFilter. The problem appears to be that the logic inServletContextInitializerBeansthat looks for an existing registration bean doesn't find the one that's been created for the@RefreshScopeFilterso it creates one and we end up with two.wilkinsona commentedon Oct 14, 2016
In the case where there's just
myFilter(and no explicit registration bean), there are two bean definitions, one namedmyFilterand one namedscopedTarget.myFilter. The latter matches when looking for beans of typeFilterbut the former does not. The former doesn't match as it's actually aScopedProxyFactoryBeanand there's insufficient type information available to match it as aFilter.When an explicit registration bean is added it has an interesting side-effect. It results in the
myFilterbean being created earlier which gives the container more to go on when it's looking for a match. It no longer seesmyFilterpurely as a definition for aScopedProxyFactoryBeanbut can now use the singleton instance for type matching. As a result, bothmyFilterandscopedTarget.myFilterare registered.wilkinsona commentedon Oct 14, 2016
There are two problems here:
FilterRegistrationBeanthe rawscopedTarget.myFilterbean is registered rather thanmyFilterwhich is the scoped proxy. This means that refresh has no effect.FilterRegistrationBeanthe filter's registered twice. The faulty implicit registration described above and explicit registration. The explicit registration registersmyFilterwith the servlet container which is the scoped proxy so one of the two filters refreshes correctly.wilkinsona commentedon Oct 14, 2016
This change fixes both problems:
However, it's relying on eager init being allowed to correctly determine the type that will be produced by the
ScopedProxyFactoryBean. I doubt that that's a good idea.dantesun commentedon Oct 14, 2016
@wilkinsona In RefreshScope, there's another flag controlling eager-init behavior. So "beanFactory.getBeanNamesForType(type, true, true)" won't eager init the "RefreshScope" bean with "eager" set to false. Am I right?
wilkinsona commentedon Oct 14, 2016
The specifics of refresh scope are only one small part of it. Allowing eager init will initialise all lazy-init singletons and factory beans as part of the lookup and we don't want to do that.
dantesun commentedon Oct 14, 2016
A second thought, I don't quite understand why you want to eager init the beans. Maybe I am missing something...
wilkinsona commentedon Oct 14, 2016
I think I explained that above:
dantesun commentedon Oct 14, 2016
Sorry, I miss that part.
wilkinsona commentedon Oct 14, 2016
Here's a small app that illustrates the problem:
Its output is:
I think we may need an enhancement to the container so that
myBeanis found in post construct which is called before theScopedProxyFactoryBeanhas been instantiated. @snicoll is on the case.[-]ServletContextInitializerBeans incorrectly handles @RefreshScope filters[/-][+]ServletContextInitializerBeans can't handle "scopedTarget." beans correctly.[/+]wilkinsona commentedon Oct 17, 2016
Issue tracking the Spring Framework enhancement: https://jira.spring.io/browse/SPR-14816
Ignore scoped targets when collecting servlet context initializer beans