Skip to content

Commit

Permalink
Expose load-on-startup for Jersey
Browse files Browse the repository at this point in the history
This commit adds a `spring.jersey.filter.load-on-startup` property used to
customize the startup priority of the Jersey servlet.

Closes spring-projectsgh-5100
  • Loading branch information
snicoll committed Mar 29, 2016
1 parent 928f2df commit c752fac
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,6 +26,7 @@
*
* @author Dave Syer
* @author Eddú Meléndez
* @author Stephane Nicoll
* @since 1.2.0
*/
@ConfigurationProperties("spring.jersey")
Expand All @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}

}

}
Original file line number Diff line number Diff line change
@@ -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 {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
14 changes: 8 additions & 6 deletions spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c752fac

Please sign in to comment.