Skip to content

Commit f823599

Browse files
committed
Replace @PostConstruct validation with setter validation
Closes gh-7579
1 parent 9a044d1 commit f823599

File tree

7 files changed

+19
-59
lines changed

7 files changed

+19
-59
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222

23-
import javax.annotation.PostConstruct;
2423
import javax.servlet.http.HttpSession;
2524

2625
import org.springframework.boot.autoconfigure.security.SecurityPrerequisite;
@@ -88,11 +87,6 @@ public class ManagementServerProperties implements SecurityPrerequisite {
8887

8988
private final Security security = new Security();
9089

91-
@PostConstruct
92-
private void validate() {
93-
Assert.notNull(this.contextPath, "ContextPath must not be null");
94-
}
95-
9690
/**
9791
* Returns the management port or {@code null} if the
9892
* {@link ServerProperties#getPort() server port} should be used.
@@ -138,6 +132,7 @@ public String getContextPath() {
138132
}
139133

140134
public void setContextPath(String contextPath) {
135+
Assert.notNull(contextPath, "ContextPath must not be null");
141136
this.contextPath = cleanContextPath(contextPath);
142137
}
143138

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/AbstractEndpoint.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import java.util.regex.Pattern;
2020

21-
import javax.annotation.PostConstruct;
22-
2321
import org.springframework.context.EnvironmentAware;
2422
import org.springframework.core.env.Environment;
2523
import org.springframework.util.Assert;
@@ -55,13 +53,6 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa
5553
*/
5654
private Boolean enabled;
5755

58-
@PostConstruct
59-
private void validate() {
60-
Assert.notNull(this.id, "Id must not be null");
61-
Assert.isTrue(ID_PATTERN.matcher(this.id).matches(),
62-
"ID must only contains letters, numbers and '_'");
63-
}
64-
6556
/**
6657
* Create a new sensitive endpoint instance. The endpoint will enabled flag will be
6758
* based on the spring {@link Environment} unless explicitly set.
@@ -78,7 +69,7 @@ public AbstractEndpoint(String id) {
7869
* @param sensitive if the endpoint is sensitive by default
7970
*/
8071
public AbstractEndpoint(String id, boolean sensitive) {
81-
this.id = id;
72+
setId(id);
8273
this.sensitiveDefault = sensitive;
8374
}
8475

@@ -89,7 +80,7 @@ public AbstractEndpoint(String id, boolean sensitive) {
8980
* @param enabled if the endpoint is enabled or not.
9081
*/
9182
public AbstractEndpoint(String id, boolean sensitive, boolean enabled) {
92-
this.id = id;
83+
setId(id);
9384
this.sensitiveDefault = sensitive;
9485
this.enabled = enabled;
9586
}
@@ -109,6 +100,9 @@ public String getId() {
109100
}
110101

111102
public void setId(String id) {
103+
Assert.notNull(id, "Id must not be null");
104+
Assert.isTrue(ID_PATTERN.matcher(id).matches(),
105+
"Id must only contains letters, numbers and '_'");
112106
this.id = id;
113107
}
114108

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/AbstractMvcEndpoint.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.endpoint.mvc;
1818

19-
import javax.annotation.PostConstruct;
20-
2119
import org.springframework.boot.actuate.endpoint.Endpoint;
2220
import org.springframework.boot.actuate.endpoint.EndpointProperties;
2321
import org.springframework.context.EnvironmentAware;
@@ -56,23 +54,16 @@ public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter
5654
private final boolean sensitiveDefault;
5755

5856
public AbstractMvcEndpoint(String path, boolean sensitive) {
59-
this.path = path;
57+
setPath(path);
6058
this.sensitiveDefault = sensitive;
6159
}
6260

6361
public AbstractMvcEndpoint(String path, boolean sensitive, boolean enabled) {
64-
this.path = path;
62+
setPath(path);
6563
this.sensitiveDefault = sensitive;
6664
this.enabled = enabled;
6765
}
6866

69-
@PostConstruct
70-
private void validate() {
71-
Assert.notNull(this.path, "Path must not be null");
72-
Assert.isTrue(this.path.isEmpty() || this.path.startsWith("/"),
73-
"Path must start with / or be empty");
74-
}
75-
7667
@Override
7768
public void setEnvironment(Environment environment) {
7869
this.environment = environment;
@@ -88,6 +79,9 @@ public String getPath() {
8879
}
8980

9081
public void setPath(String path) {
82+
Assert.notNull(path, "Path must not be null");
83+
Assert.isTrue(path.isEmpty() || path.startsWith("/"),
84+
"Path must start with / or be empty");
9185
this.path = path;
9286
}
9387

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.h2;
1818

19-
import javax.annotation.PostConstruct;
20-
2119
import org.springframework.boot.context.properties.ConfigurationProperties;
2220
import org.springframework.util.Assert;
2321

@@ -44,18 +42,14 @@ public class H2ConsoleProperties {
4442

4543
private final Settings settings = new Settings();
4644

47-
@PostConstruct
48-
private void validate() {
49-
Assert.notNull(this.path, "Path must not be null");
50-
Assert.isTrue(this.path.isEmpty() || this.path.startsWith("/"),
51-
"Path must start with / or be empty");
52-
}
53-
5445
public String getPath() {
5546
return this.path;
5647
}
5748

5849
public void setPath(String path) {
50+
Assert.notNull(path, "Path must not be null");
51+
Assert.isTrue(path.isEmpty() || path.startsWith("/"),
52+
"Path must start with / or be empty");
5953
this.path = path;
6054
}
6155

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.io.File;
2020
import java.util.Map;
2121

22-
import javax.annotation.PostConstruct;
23-
2422
import liquibase.integration.spring.SpringLiquibase;
2523

2624
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -96,16 +94,12 @@ public class LiquibaseProperties {
9694
*/
9795
private File rollbackFile;
9896

99-
@PostConstruct
100-
private void validate() {
101-
Assert.notNull(this.changeLog, "ChangeLog must not be null");
102-
}
103-
10497
public String getChangeLog() {
10598
return this.changeLog;
10699
}
107100

108101
public void setChangeLog(String changeLog) {
102+
Assert.notNull(changeLog, "ChangeLog must not be null");
109103
this.changeLog = changeLog;
110104
}
111105

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Map;
2727
import java.util.Set;
2828

29-
import javax.annotation.PostConstruct;
3029
import javax.servlet.ServletContext;
3130
import javax.servlet.ServletException;
3231
import javax.servlet.SessionCookieConfig;
@@ -176,11 +175,6 @@ public class ServerProperties
176175

177176
private Environment environment;
178177

179-
@PostConstruct
180-
private void validate() {
181-
Assert.notNull(this.servletPath, "ServletPath must not be null");
182-
}
183-
184178
@Override
185179
public int getOrder() {
186180
return 0;
@@ -336,6 +330,7 @@ public String getServletPath() {
336330
}
337331

338332
public void setServletPath(String servletPath) {
333+
Assert.notNull(servletPath, "ServletPath must not be null");
339334
this.servletPath = servletPath;
340335
}
341336

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesProperties.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22-
import javax.annotation.PostConstruct;
23-
2422
import org.springframework.boot.context.properties.ConfigurationProperties;
2523
import org.springframework.util.Assert;
2624

@@ -41,18 +39,14 @@ public class WebServicesProperties {
4139

4240
private final Servlet servlet = new Servlet();
4341

44-
@PostConstruct
45-
private void validate() {
46-
Assert.notNull(this.path, "Path must not be null");
47-
Assert.isTrue(this.path.isEmpty() || this.path.startsWith("/"),
48-
"Path must start with / or be empty");
49-
}
50-
5142
public String getPath() {
5243
return this.path;
5344
}
5445

5546
public void setPath(String path) {
47+
Assert.notNull(path, "Path must not be null");
48+
Assert.isTrue(path.isEmpty() || path.startsWith("/"),
49+
"Path must start with / or be empty");
5650
this.path = path;
5751
}
5852

0 commit comments

Comments
 (0)