Ensure ServerProperties default values does not override

Since ServerProperties had primitive properties for port (in
particular) it was not possible to check when applying those
properties if the user had actually changed the value. This
in turn meant that a custom EmbeddedServletContainerFactory
could not set the default values.

Fixed by making int properties of ServerProperties into
Integer and checking for null before setting on the
container factory.

Fixes gh-84
This commit is contained in:
Dave Syer
2013-10-10 09:43:07 -04:00
parent b855ab6cae
commit 2c60828cf2
3 changed files with 89 additions and 16 deletions

View File

@@ -47,11 +47,11 @@ import org.springframework.util.StringUtils;
@ConfigurationProperties(name = "server", ignoreUnknownFields = false)
public class ServerProperties implements EmbeddedServletContainerCustomizer {
private int port = 8080;
private Integer port;
private InetAddress address;
private int sessionTimeout = 30;
private Integer sessionTimeout;
@NotNull
private String contextPath = "";
@@ -70,11 +70,11 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
this.contextPath = contextPath;
}
public int getPort() {
public Integer getPort() {
return this.port;
}
public void setPort(int port) {
public void setPort(Integer port) {
this.port = port;
}
@@ -86,11 +86,11 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
this.address = address;
}
public int getSessionTimeout() {
public Integer getSessionTimeout() {
return this.sessionTimeout;
}
public void setSessionTimeout(int sessionTimeout) {
public void setSessionTimeout(Integer sessionTimeout) {
this.sessionTimeout = sessionTimeout;
}
@@ -100,10 +100,18 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
factory.setPort(getPort());
factory.setAddress(getAddress());
factory.setContextPath(getContextPath());
factory.setSessionTimeout(getSessionTimeout());
if (getPort() != null) {
factory.setPort(getPort());
}
if (getAddress() != null) {
factory.setAddress(getAddress());
}
if (getContextPath() != null) {
factory.setContextPath(getContextPath());
}
if (getSessionTimeout() != null) {
factory.setSessionTimeout(getSessionTimeout());
}
if (factory instanceof TomcatEmbeddedServletContainerFactory) {
getTomcat().customizeTomcat((TomcatEmbeddedServletContainerFactory) factory);
}

View File

@@ -24,7 +24,6 @@ import java.util.Map;
import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.boot.context.embedded.properties.ServerProperties;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -51,7 +50,7 @@ public class ServerPropertiesTests {
public void testPortBinding() throws Exception {
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
Collections.singletonMap("server.port", "9000")));
assertEquals(9000, this.properties.getPort());
assertEquals(9000, this.properties.getPort().intValue());
}
@Test