From 40427cdb8275f0b556d6fb6e99983ebb4daf4d4e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 30 Nov 2015 17:57:53 +0000 Subject: [PATCH] Separate Jersey WebApplicationInitializer from auto-configuration class Previously, JerseyAutoConfiguration was a WebApplicationInitializer. This was problematic as auto-configuration classes should not be ordered (they should use AutoConfigureBefore etc instead) but the web application initializer needs to be ordered so that it can run early and configure Jersey before it runs. This commit has moved the WebApplicationInitializer implementation into a separate class so that it can be ordered independently of the auto-configuration class. Note that the new class must be public for the servlet container (Tomcat at least) to be able to instantiate it. Closes gh-4527 --- .../jersey/JerseyAutoConfiguration.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java index 6793b95825..09386849b2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java @@ -48,6 +48,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.Order; import org.springframework.util.StringUtils; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.filter.RequestContextFilter; @@ -67,7 +68,7 @@ import org.springframework.web.filter.RequestContextFilter; @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @AutoConfigureBefore(DispatcherServletAutoConfiguration.class) @EnableConfigurationProperties(JerseyProperties.class) -public class JerseyAutoConfiguration implements WebApplicationInitializer { +public class JerseyAutoConfiguration { @Autowired private JerseyProperties jersey; @@ -138,13 +139,6 @@ public class JerseyAutoConfiguration implements WebApplicationInitializer { } } - @Override - public void onStartup(ServletContext servletContext) throws ServletException { - // We need to switch *off* the Jersey WebApplicationInitializer because it - // will try and register a ContextLoaderListener which we don't need - servletContext.setInitParameter("contextConfigLocation", ""); - } - private static String findApplicationPath(ApplicationPath annotation) { // Jersey doesn't like to be the default servlet, so map to /* as a fallback if (annotation == null) { @@ -160,4 +154,17 @@ public class JerseyAutoConfiguration implements WebApplicationInitializer { return applicationPath.equals("/") ? "/*" : applicationPath + "/*"; } + @Order(Ordered.HIGHEST_PRECEDENCE) + public static final class JerseyWebApplicationInitializer + implements WebApplicationInitializer { + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + // We need to switch *off* the Jersey WebApplicationInitializer because it + // will try and register a ContextLoaderListener which we don't need + servletContext.setInitParameter("contextConfigLocation", ""); + } + + } + }