Commit c752fac5 authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent 928f2dfc
......@@ -75,6 +75,7 @@ import org.springframework.web.filter.RequestContextFilter;
* @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 class JerseyAutoConfiguration implements ServletContextAware {
new ServletContainer(this.config), this.path);
addInitParameters(registration);
registration.setName(getServletRegistrationName());
registration.setLoadOnStartup(this.jersey.getServlet().getLoadOnStartup());
return registration;
}
......
/*
* 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 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
*
* @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<String, String> init = new HashMap<String, String>();
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 class JerseyProperties {
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 class JerseyProperties {
}
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;
}
}
}
/*
* 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 {
}
}
......@@ -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])
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment