Polish auto-configuration for Spring Web Services
Closes gh-5645
This commit is contained in:
@@ -29,8 +29,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.ws.config.annotation.DelegatingWsConfiguration;
|
||||
import org.springframework.ws.config.annotation.EnableWs;
|
||||
import org.springframework.ws.config.annotation.WsConfigurationSupport;
|
||||
import org.springframework.ws.transport.http.MessageDispatcherServlet;
|
||||
|
||||
@@ -38,6 +37,7 @@ import org.springframework.ws.transport.http.MessageDispatcherServlet;
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Web Services.
|
||||
*
|
||||
* @author Vedran Pavic
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.4.0
|
||||
*/
|
||||
@Configuration
|
||||
@@ -48,7 +48,7 @@ import org.springframework.ws.transport.http.MessageDispatcherServlet;
|
||||
@AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class)
|
||||
public class WsAutoConfiguration {
|
||||
|
||||
private WsProperties properties;
|
||||
private final WsProperties properties;
|
||||
|
||||
public WsAutoConfiguration(WsProperties properties) {
|
||||
this.properties = properties;
|
||||
@@ -63,15 +63,16 @@ public class WsAutoConfiguration {
|
||||
String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*");
|
||||
ServletRegistrationBean registration = new ServletRegistrationBean(
|
||||
servlet, urlMapping);
|
||||
registration.setLoadOnStartup(this.properties.getServlet().getLoadOnStartup());
|
||||
for (Map.Entry<String, String> entry : this.properties.getInit().entrySet()) {
|
||||
WsProperties.Servlet servletProperties = this.properties.getServlet();
|
||||
registration.setLoadOnStartup(servletProperties.getLoadOnStartup());
|
||||
for (Map.Entry<String, String> entry : servletProperties.getInit().entrySet()) {
|
||||
registration.addInitParameter(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return registration;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(DelegatingWsConfiguration.class)
|
||||
@EnableWs
|
||||
protected static class WsConfiguration {
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
* {@link ConfigurationProperties} for Spring Web Services.
|
||||
*
|
||||
* @author Vedran Pavic
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.4.0
|
||||
*/
|
||||
@ConfigurationProperties("spring.ws")
|
||||
@@ -40,11 +41,6 @@ public class WsProperties {
|
||||
@Pattern(regexp = "/[^?#]*", message = "Path must start with /")
|
||||
private String path = "/services";
|
||||
|
||||
/**
|
||||
* Init parameters to pass to Spring Web Services via the servlet.
|
||||
*/
|
||||
private Map<String, String> init = new HashMap<String, String>();
|
||||
|
||||
private final Servlet servlet = new Servlet();
|
||||
|
||||
public String getPath() {
|
||||
@@ -55,25 +51,31 @@ public class WsProperties {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public Map<String, String> getInit() {
|
||||
return this.init;
|
||||
}
|
||||
|
||||
public void setInit(Map<String, String> init) {
|
||||
this.init = init;
|
||||
}
|
||||
|
||||
public Servlet getServlet() {
|
||||
return this.servlet;
|
||||
}
|
||||
|
||||
|
||||
public static class Servlet {
|
||||
|
||||
/**
|
||||
* Servlet init parameters to pass to Spring Web Services.
|
||||
*/
|
||||
private Map<String, String> init = new HashMap<String, String>();
|
||||
|
||||
/**
|
||||
* Load on startup priority of the Spring Web Services servlet.
|
||||
*/
|
||||
private int loadOnStartup = -1;
|
||||
|
||||
public Map<String, String> getInit() {
|
||||
return this.init;
|
||||
}
|
||||
|
||||
public void setInit(Map<String, String> init) {
|
||||
this.init = init;
|
||||
}
|
||||
|
||||
public int getLoadOnStartup() {
|
||||
return this.loadOnStartup;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.boot.autoconfigure.ws;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
@@ -35,18 +34,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link WsAutoConfiguration}.
|
||||
*
|
||||
* @author Vedran Pavic
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class WsAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setupContext() {
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
}
|
||||
private AnnotationConfigWebApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
@@ -57,7 +52,7 @@ public class WsAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void defaultConfiguration() {
|
||||
registerAndRefresh(WsAutoConfiguration.class);
|
||||
load(WsAutoConfiguration.class);
|
||||
|
||||
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
|
||||
}
|
||||
@@ -66,27 +61,19 @@ public class WsAutoConfigurationTests {
|
||||
public void customPathMustBeginWithASlash() {
|
||||
this.thrown.expect(BeanCreationException.class);
|
||||
this.thrown.expectMessage("Path must start with /");
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.ws.path=invalid");
|
||||
registerAndRefresh(WsAutoConfiguration.class);
|
||||
load(WsAutoConfiguration.class, "spring.ws.path=invalid");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customPathWithTrailingSlash() {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.ws.path=/valid/");
|
||||
registerAndRefresh(WsAutoConfiguration.class);
|
||||
|
||||
load(WsAutoConfiguration.class, "spring.ws.path=/valid/");
|
||||
assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings())
|
||||
.contains("/valid/*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customPath() {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.ws.path=/valid");
|
||||
registerAndRefresh(WsAutoConfiguration.class);
|
||||
|
||||
load(WsAutoConfiguration.class, "spring.ws.path=/valid");
|
||||
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
|
||||
assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings())
|
||||
.contains("/valid/*");
|
||||
@@ -94,10 +81,7 @@ public class WsAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void customLoadOnStartup() {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.ws.servlet.load-on-startup=1");
|
||||
registerAndRefresh(WsAutoConfiguration.class);
|
||||
|
||||
load(WsAutoConfiguration.class, "spring.ws.servlet.load-on-startup=1");
|
||||
ServletRegistrationBean registrationBean = this.context
|
||||
.getBean(ServletRegistrationBean.class);
|
||||
assertThat(ReflectionTestUtils.getField(registrationBean, "loadOnStartup"))
|
||||
@@ -106,19 +90,21 @@ public class WsAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void customInitParameters() {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.ws.init.key1=value1", "spring.ws.init.key2=value2");
|
||||
registerAndRefresh(WsAutoConfiguration.class);
|
||||
|
||||
load(WsAutoConfiguration.class, "spring.ws.servlet.init.key1=value1",
|
||||
"spring.ws.servlet.init.key2=value2");
|
||||
ServletRegistrationBean registrationBean = this.context
|
||||
.getBean(ServletRegistrationBean.class);
|
||||
assertThat(registrationBean.getInitParameters()).containsEntry("key1", "value1");
|
||||
assertThat(registrationBean.getInitParameters()).containsEntry("key2", "value2");
|
||||
}
|
||||
|
||||
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
||||
this.context.register(annotatedClasses);
|
||||
this.context.refresh();
|
||||
private void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
|
||||
ctx.setServletContext(new MockServletContext());
|
||||
EnvironmentTestUtils.addEnvironment(ctx, environment);
|
||||
ctx.register(config);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user