From c752fac5c0cddd60b80f151751ed72fba71f8c85 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 29 Mar 2016 14:18:18 +0200 Subject: [PATCH] Expose load-on-startup for Jersey This commit adds a `spring.jersey.filter.load-on-startup` property used to customize the startup priority of the Jersey servlet. Closes gh-5100 --- .../jersey/JerseyAutoConfiguration.java | 2 + .../jersey/JerseyProperties.java | 28 ++++++- ...ConfigurationCustomLoadOnStartupTests.java | 81 +++++++++++++++++++ .../appendix-application-properties.adoc | 1 + .../main/asciidoc/spring-boot-features.adoc | 14 ++-- 5 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomLoadOnStartupTests.java 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 9c31d09851ed..a3a18bc9c8d9 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 @@ -75,6 +75,7 @@ * @author Dave Syer * @author Andy Wilkinson * @author Eddú Meléndez + * @author Stephane Nicoll */ @Configuration @ConditionalOnClass(name = { "org.glassfish.jersey.server.spring.SpringComponentProvider", @@ -170,6 +171,7 @@ public ServletRegistrationBean jerseyServletRegistration() { new ServletContainer(this.config), this.path); addInitParameters(registration); registration.setName(getServletRegistrationName()); + registration.setLoadOnStartup(this.jersey.getServlet().getLoadOnStartup()); return registration; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyProperties.java index 4451ce4201fb..686bce55ca78 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ * * @author Dave Syer * @author Eddú Meléndez + * @author Stephane Nicoll * @since 1.2.0 */ @ConfigurationProperties("spring.jersey") @@ -41,7 +42,9 @@ public class JerseyProperties { */ private Map init = new HashMap(); - private Filter filter = new Filter(); + private final Filter filter = new Filter(); + + private final Servlet servlet = new Servlet(); /** * Path that serves as the base URI for the application. Overrides the value of @@ -53,8 +56,8 @@ public Filter getFilter() { return this.filter; } - public void setFilter(Filter filter) { - this.filter = filter; + public Servlet getServlet() { + return this.servlet; } public Type getType() { @@ -102,4 +105,21 @@ public void setOrder(int order) { } + public static class Servlet { + + /** + * Load on startup priority of the Jersey servlet. + */ + private int loadOnStartup = -1; + + public int getLoadOnStartup() { + return this.loadOnStartup; + } + + public void setLoadOnStartup(int loadOnStartup) { + this.loadOnStartup = loadOnStartup; + } + + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomLoadOnStartupTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomLoadOnStartupTests.java new file mode 100644 index 000000000000..85576b5a212c --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomLoadOnStartupTests.java @@ -0,0 +1,81 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jersey; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.glassfish.jersey.server.ResourceConfig; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationCustomLoadOnStartupTests.Application; +import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; +import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.test.context.SpringApplicationConfiguration; +import org.springframework.boot.test.context.web.WebIntegrationTest; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link JerseyAutoConfiguration} when using custom load on startup. + * @author Stephane Nicoll + */ +@RunWith(SpringRunner.class) +@DirtiesContext +@SpringApplicationConfiguration(Application.class) +@WebIntegrationTest(randomPort = true, value = "spring.jersey.servlet.load-on-startup=5") +public class JerseyAutoConfigurationCustomLoadOnStartupTests { + + @Autowired + private ApplicationContext context; + + @Test + public void contextLoads() { + assertThat(new DirectFieldAccessor(this.context.getBean("jerseyServletRegistration")) + .getPropertyValue("loadOnStartup")).isEqualTo(5); + } + + @MinimalWebConfiguration + public static class Application extends ResourceConfig { + + public Application() { + register(Application.class); + } + + } + + @Target(ElementType.TYPE) + @Retention(RetentionPolicy.RUNTIME) + @Documented + @Import({EmbeddedServletContainerAutoConfiguration.class, + ServerPropertiesAutoConfiguration.class, JerseyAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class}) + protected @interface MinimalWebConfiguration { + } + +} diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 804da5b3f7a9..afad598c8b0b 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -284,6 +284,7 @@ content into your application; rather pick only the properties that you need. spring.jersey.application-path= # Path that serves as the base URI for the application. Overrides the value of "@ApplicationPath" if specified. spring.jersey.filter.order=0 # Jersey filter chain order. spring.jersey.init.*= # Init parameters to pass to Jersey via the servlet or filter. + spring.jersey.servlet.load-on-startup=-1 # Load on startup priority of the Jersey servlet. spring.jersey.type=servlet # Jersey integration type. Can be either "servlet" or "filter". # SPRING MOBILE DEVICE VIEWS ({sc-spring-boot-autoconfigure}/mobile/DeviceDelegatingViewResolverAutoConfiguration.{sc-ext}[DeviceDelegatingViewResolverAutoConfiguration]) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 1d4963439ba1..7afcb2910103 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1859,12 +1859,14 @@ servlet will be registered and mapped to `/*` by default. You can change the map by adding `@ApplicationPath` to your `ResourceConfig`. By default Jersey will be set up as a Servlet in a `@Bean` of type -`ServletRegistrationBean` named `jerseyServletRegistration`. You can disable or override -that bean by creating one of your own with the same name. You can also use a Filter -instead of a Servlet by setting `spring.jersey.type=filter` (in which case the `@Bean` to -replace or override is `jerseyFilterRegistration`). The servlet has an `@Order` which you -can set with `spring.jersey.filter.order`. Both the Servlet and the Filter registrations -can be given init parameters using `spring.jersey.init.*` to specify a map of properties. +`ServletRegistrationBean` named `jerseyServletRegistration`. By default, the servlet will +be initialized lazily but you can customize it with +`spring.jersey.servlet.load-on-startup` .You can disable or override that bean by creating +one of your own with the same name. You can also use a Filter instead of a Servlet by +setting `spring.jersey.type=filter` (in which case the `@Bean` to replace or override is +`jerseyFilterRegistration`). The servlet has an `@Order` which you can set with +`spring.jersey.filter.order`. Both the Servlet and the Filter registrations can be given +init parameters using `spring.jersey.init.*` to specify a map of properties. There is a {github-code}/spring-boot-samples/spring-boot-sample-jersey[Jersey sample] so you can see how to set things up. There is also a