Replace @PostConstruct validation with setter validation

Closes gh-7579
This commit is contained in:
Andy Wilkinson 2017-01-26 09:29:58 +00:00
parent 9a044d1577
commit f823599d1f
7 changed files with 19 additions and 59 deletions

View File

@ -20,7 +20,6 @@ import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import org.springframework.boot.autoconfigure.security.SecurityPrerequisite;
@ -88,11 +87,6 @@ public class ManagementServerProperties implements SecurityPrerequisite {
private final Security security = new Security();
@PostConstruct
private void validate() {
Assert.notNull(this.contextPath, "ContextPath must not be null");
}
/**
* Returns the management port or {@code null} if the
* {@link ServerProperties#getPort() server port} should be used.
@ -138,6 +132,7 @@ public class ManagementServerProperties implements SecurityPrerequisite {
}
public void setContextPath(String contextPath) {
Assert.notNull(contextPath, "ContextPath must not be null");
this.contextPath = cleanContextPath(contextPath);
}

View File

@ -18,8 +18,6 @@ package org.springframework.boot.actuate.endpoint;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
@ -55,13 +53,6 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa
*/
private Boolean enabled;
@PostConstruct
private void validate() {
Assert.notNull(this.id, "Id must not be null");
Assert.isTrue(ID_PATTERN.matcher(this.id).matches(),
"ID must only contains letters, numbers and '_'");
}
/**
* Create a new sensitive endpoint instance. The endpoint will enabled flag will be
* based on the spring {@link Environment} unless explicitly set.
@ -78,7 +69,7 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa
* @param sensitive if the endpoint is sensitive by default
*/
public AbstractEndpoint(String id, boolean sensitive) {
this.id = id;
setId(id);
this.sensitiveDefault = sensitive;
}
@ -89,7 +80,7 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa
* @param enabled if the endpoint is enabled or not.
*/
public AbstractEndpoint(String id, boolean sensitive, boolean enabled) {
this.id = id;
setId(id);
this.sensitiveDefault = sensitive;
this.enabled = enabled;
}
@ -109,6 +100,9 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa
}
public void setId(String id) {
Assert.notNull(id, "Id must not be null");
Assert.isTrue(ID_PATTERN.matcher(id).matches(),
"Id must only contains letters, numbers and '_'");
this.id = id;
}

View File

@ -16,8 +16,6 @@
package org.springframework.boot.actuate.endpoint.mvc;
import javax.annotation.PostConstruct;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EndpointProperties;
import org.springframework.context.EnvironmentAware;
@ -56,23 +54,16 @@ public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter
private final boolean sensitiveDefault;
public AbstractMvcEndpoint(String path, boolean sensitive) {
this.path = path;
setPath(path);
this.sensitiveDefault = sensitive;
}
public AbstractMvcEndpoint(String path, boolean sensitive, boolean enabled) {
this.path = path;
setPath(path);
this.sensitiveDefault = sensitive;
this.enabled = enabled;
}
@PostConstruct
private void validate() {
Assert.notNull(this.path, "Path must not be null");
Assert.isTrue(this.path.isEmpty() || this.path.startsWith("/"),
"Path must start with / or be empty");
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
@ -88,6 +79,9 @@ public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter
}
public void setPath(String path) {
Assert.notNull(path, "Path must not be null");
Assert.isTrue(path.isEmpty() || path.startsWith("/"),
"Path must start with / or be empty");
this.path = path;
}

View File

@ -16,8 +16,6 @@
package org.springframework.boot.autoconfigure.h2;
import javax.annotation.PostConstruct;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;
@ -44,18 +42,14 @@ public class H2ConsoleProperties {
private final Settings settings = new Settings();
@PostConstruct
private void validate() {
Assert.notNull(this.path, "Path must not be null");
Assert.isTrue(this.path.isEmpty() || this.path.startsWith("/"),
"Path must start with / or be empty");
}
public String getPath() {
return this.path;
}
public void setPath(String path) {
Assert.notNull(path, "Path must not be null");
Assert.isTrue(path.isEmpty() || path.startsWith("/"),
"Path must start with / or be empty");
this.path = path;
}

View File

@ -19,8 +19,6 @@ package org.springframework.boot.autoconfigure.liquibase;
import java.io.File;
import java.util.Map;
import javax.annotation.PostConstruct;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -96,16 +94,12 @@ public class LiquibaseProperties {
*/
private File rollbackFile;
@PostConstruct
private void validate() {
Assert.notNull(this.changeLog, "ChangeLog must not be null");
}
public String getChangeLog() {
return this.changeLog;
}
public void setChangeLog(String changeLog) {
Assert.notNull(changeLog, "ChangeLog must not be null");
this.changeLog = changeLog;
}

View File

@ -26,7 +26,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.SessionCookieConfig;
@ -176,11 +175,6 @@ public class ServerProperties
private Environment environment;
@PostConstruct
private void validate() {
Assert.notNull(this.servletPath, "ServletPath must not be null");
}
@Override
public int getOrder() {
return 0;
@ -336,6 +330,7 @@ public class ServerProperties
}
public void setServletPath(String servletPath) {
Assert.notNull(servletPath, "ServletPath must not be null");
this.servletPath = servletPath;
}

View File

@ -19,8 +19,6 @@ package org.springframework.boot.autoconfigure.webservices;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;
@ -41,18 +39,14 @@ public class WebServicesProperties {
private final Servlet servlet = new Servlet();
@PostConstruct
private void validate() {
Assert.notNull(this.path, "Path must not be null");
Assert.isTrue(this.path.isEmpty() || this.path.startsWith("/"),
"Path must start with / or be empty");
}
public String getPath() {
return this.path;
}
public void setPath(String path) {
Assert.notNull(path, "Path must not be null");
Assert.isTrue(path.isEmpty() || path.startsWith("/"),
"Path must start with / or be empty");
this.path = path;
}