Description
Spring Boot 1.4.0.M3 (and prior versions) initialize Servlet Filters too eagerly. This can lead to inconsistencies between running tests and integration tests (i.e. spring-projects/spring-data-examples#189). It can also lead to ordering issues when working with AOP (i.e. #2578 behaves differently between test and integration test).
The problem is cause by:
- AbstractApplicationContext invokes onRefresh
- EmbeddedWebApplicationContext implements
onRefresh
by invoking createEmbeddedServletContainer - When using Tomcat, TomcatEmbeddedServletContainer will start Tomcat
- This causes all the Servlet Container Beans (i.e. Filter, Servlet, etc) to be instantiated eagerly in getServletContextInitializerBeans.
This is not ideal because these Beans might depend on a lot of other Beans (i.e. Servlet initializes all of Spring MVC, Filter initializes Spring Security, etc) and cause all sorts of ordering issues due to the eager initialization of the Beans. It should be noted that while DelegatingFilterProxy
is used, the servlet container initializes the DelegatingFilterProxy
which in turn initializes the Filter
that is marked as a Bean.
As it stands onRefresh initializes every Bean rather than "special Beans" (as is intended).
What is expected is that the Servlet Container Beans would be initialized in finishBeanFactoryInitialization.