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
This commit is contained in:
Andy Wilkinson 2015-11-30 17:57:53 +00:00
parent 6ec767437a
commit 40427cdb82
1 changed files with 15 additions and 8 deletions

View File

@ -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", "<NONE>");
}
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", "<NONE>");
}
}
}