From f823599d1fed8e1c8aa949c02d18bcd67779f72f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 26 Jan 2017 09:29:58 +0000 Subject: [PATCH] Replace @PostConstruct validation with setter validation Closes gh-7579 --- .../ManagementServerProperties.java | 7 +------ .../boot/actuate/endpoint/AbstractEndpoint.java | 16 +++++----------- .../endpoint/mvc/AbstractMvcEndpoint.java | 16 +++++----------- .../autoconfigure/h2/H2ConsoleProperties.java | 12 +++--------- .../liquibase/LiquibaseProperties.java | 8 +------- .../boot/autoconfigure/web/ServerProperties.java | 7 +------ .../webservices/WebServicesProperties.java | 12 +++--------- 7 files changed, 19 insertions(+), 59 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java index 328cb740181..b6bd2e036db 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java @@ -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); } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java index cb7ba3b55eb..a81c2e99d7b 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java @@ -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 implements Endpoint, 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 implements Endpoint, 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 implements Endpoint, 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 implements Endpoint, 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; } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/AbstractMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/AbstractMvcEndpoint.java index dfee586dee4..55b73530a55 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/AbstractMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/AbstractMvcEndpoint.java @@ -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; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java index dcc931e5930..10f851bd94d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java @@ -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; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java index c72de28029a..c6176b5368a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java @@ -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; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 21c6d257753..9830d0e87db 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -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; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesProperties.java index 87a57a2cfaf..4f26ef282fa 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesProperties.java @@ -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; }