diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/EndpointServlet.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/EndpointServlet.java index 44239b2fde55..a72d0508fbcf 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/EndpointServlet.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/EndpointServlet.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -35,23 +35,31 @@ public final class EndpointServlet { private final Servlet servlet; + private final int loadOnStartup; + private final Map initParameters; public EndpointServlet(Class servlet) { Assert.notNull(servlet, "Servlet must not be null"); this.servlet = BeanUtils.instantiateClass(servlet); this.initParameters = Collections.emptyMap(); + this.loadOnStartup = -1; + } public EndpointServlet(Servlet servlet) { Assert.notNull(servlet, "Servlet must not be null"); this.servlet = servlet; this.initParameters = Collections.emptyMap(); + this.loadOnStartup = -1; } - private EndpointServlet(Servlet servlet, Map initParameters) { + private EndpointServlet(Servlet servlet, Map initParameters, + int loadOnStartup) { this.servlet = servlet; this.initParameters = Collections.unmodifiableMap(initParameters); + this.loadOnStartup = loadOnStartup; + } public EndpointServlet withInitParameter(String name, String value) { @@ -67,7 +75,23 @@ public EndpointServlet withInitParameters(Map initParameters) { Map mergedInitParameters = new LinkedHashMap<>( this.initParameters); mergedInitParameters.putAll(initParameters); - return new EndpointServlet(this.servlet, mergedInitParameters); + return new EndpointServlet(this.servlet, mergedInitParameters, + this.loadOnStartup); + } + + /** + * Sets the loadOnStartup priority that will be set on Servlet + * registration + *

+ * The default value for loadOnStartup is -1. + * @param loadOnStartup the initialization priority of the Servlet + * @return a new instance of {@link EndpointServlet} with the provided + * loadOnStartup value set + * @since 2.2.0 + * @see ServletRegistration.Dynamic#setLoadOnStartup(int) + */ + public EndpointServlet withLoadOnStartup(int loadOnStartup) { + return new EndpointServlet(this.servlet, this.initParameters, loadOnStartup); } Servlet getServlet() { @@ -78,4 +102,8 @@ Map getInitParameters() { return this.initParameters; } + int getLoadOnStartup() { + return this.loadOnStartup; + } + } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrar.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrar.java index dec24a8794e8..ade3db9a9e8b 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrar.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -75,6 +75,7 @@ private void register(ServletContext servletContext, endpointServlet.getServlet()); registration.addMapping(urlMapping); registration.setInitParameters(endpointServlet.getInitParameters()); + registration.setLoadOnStartup(endpointServlet.getLoadOnStartup()); logger.info("Registered '" + path + "' to " + name); } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/EndpointServletTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/EndpointServletTests.java index 229d651b371f..1c487bb32566 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/EndpointServletTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/EndpointServletTests.java @@ -132,6 +132,32 @@ public void withInitParametersWhenHasExistingShouldMergeParameters() { } + @Test + public void withLoadOnStartupNotSetShouldReturnDefaultValue() { + EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class); + assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(-1); + } + + @Test + public void withLoadOnStartupSetShouldReturnValue() { + final int loadOnStartupTestValue = 3; + EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class) + .withLoadOnStartup(loadOnStartupTestValue); + assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(loadOnStartupTestValue); + } + + @Test + public void withLoadOnStartupAndInitParamsShouldReturnValue() { + final int loadOnStartupTestValue = 9; + EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class) + .withLoadOnStartup(loadOnStartupTestValue).withInitParameter("a", "b") + .withInitParameter("c", "d"); + Map extra = new LinkedHashMap<>(); + extra.put("a", "b1"); + extra.put("e", "f"); + assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(loadOnStartupTestValue); + } + private static class TestServlet extends GenericServlet { @Override diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrarTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrarTests.java index 399ace063d4b..3163bf8c09ea 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrarTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrarTests.java @@ -120,6 +120,30 @@ public void onStartupWhenHasInitParametersShouldRegisterInitParameters() verify(this.dynamic).setInitParameters(Collections.singletonMap("a", "b")); } + @Test + public void onStartupWhenHasLoadOnStartupShouldRegisterLoadOnStartup() + throws Exception { + final int loadOnStartupTestValue = 7; + ExposableServletEndpoint endpoint = mockEndpoint( + new EndpointServlet(TestServlet.class) + .withLoadOnStartup(loadOnStartupTestValue)); + ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator", + Collections.singleton(endpoint)); + registrar.onStartup(this.servletContext); + verify(this.dynamic).setLoadOnStartup(loadOnStartupTestValue); + } + + @Test + public void onStartupWhenHasNotLoadOnStartupShouldRegisterDefaultValue() + throws Exception { + ExposableServletEndpoint endpoint = mockEndpoint( + new EndpointServlet(TestServlet.class)); + ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator", + Collections.singleton(endpoint)); + registrar.onStartup(this.servletContext); + verify(this.dynamic).setLoadOnStartup(-1); + } + private ExposableServletEndpoint mockEndpoint(EndpointServlet endpointServlet) { ExposableServletEndpoint endpoint = mock(ExposableServletEndpoint.class); given(endpoint.getEndpointId()).willReturn(EndpointId.of("test"));