Skip to content

Commit 770c634

Browse files
committed
Polish usage of Collection in @ConfigurationProperties classes
1. It's safe to use immutable Collection object as default value of field. 2. It's unnecessary to create new Collection object in setter method. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 91da8c5 commit 770c634

File tree

11 files changed

+22
-34
lines changed

11 files changed

+22
-34
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsProperties.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@
1818

1919
import java.io.File;
2020
import java.time.Duration;
21-
import java.util.ArrayList;
22-
import java.util.Collections;
2321
import java.util.LinkedHashMap;
2422
import java.util.List;
2523
import java.util.Map;
@@ -202,7 +200,7 @@ public static class Diskspace {
202200
/**
203201
* List of paths to report disk metrics for.
204202
*/
205-
private List<File> paths = new ArrayList<>(Collections.singletonList(new File(".")));
203+
private List<File> paths = List.of(new File("."));
206204

207205
public List<File> getPaths() {
208206
return this.paths;

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/exchanges/HttpExchangesProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,7 +53,7 @@ public static class Recording {
5353
* (excluding Authorization and Cookie), response headers (excluding Set-Cookie),
5454
* and time taken.
5555
*/
56-
private Set<Include> include = new HashSet<>(Include.defaultIncludes());
56+
private Set<Include> include = Include.defaultIncludes();
5757

5858
public Set<Include> getInclude() {
5959
return this.include;

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.nio.charset.StandardCharsets;
2121
import java.time.Duration;
2222
import java.time.temporal.ChronoUnit;
23-
import java.util.ArrayList;
2423
import java.util.List;
2524

2625
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -44,7 +43,7 @@ public class MessageSourceProperties {
4443
* locations. If it doesn't contain a package qualifier (such as "org.mypackage"), it
4544
* will be resolved from the classpath root.
4645
*/
47-
private List<String> basename = new ArrayList<>(List.of("messages"));
46+
private List<String> basename = List.of("messages");
4847

4948
/**
5049
* List of locale-independent property file resources containing common messages.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@ public class ElasticsearchProperties {
3535
/**
3636
* List of the Elasticsearch instances to use.
3737
*/
38-
private List<String> uris = new ArrayList<>(Collections.singletonList("http://localhost:9200"));
38+
private List<String> uris = List.of("http://localhost:9200");
3939

4040
/**
4141
* Username for authentication with Elasticsearch.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.time.Duration;
2323
import java.time.temporal.ChronoUnit;
2424
import java.util.ArrayList;
25-
import java.util.Collections;
2625
import java.util.HashMap;
2726
import java.util.List;
2827
import java.util.Map;
@@ -57,7 +56,7 @@ public class FlywayProperties {
5756
* Locations of migrations scripts. Can contain the special "{vendor}" placeholder to
5857
* use vendor-specific locations.
5958
*/
60-
private List<String> locations = new ArrayList<>(Collections.singletonList("classpath:db/migration"));
59+
private List<String> locations = List.of("classpath:db/migration");
6160

6261
/**
6362
* Encoding of SQL migrations.
@@ -157,7 +156,7 @@ public class FlywayProperties {
157156
/**
158157
* File name suffix for SQL migrations.
159158
*/
160-
private List<String> sqlMigrationSuffixes = new ArrayList<>(Collections.singleton(".sql"));
159+
private List<String> sqlMigrationSuffixes = List.of(".sql");
161160

162161
/**
163162
* File name separator for SQL migrations.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaProperties.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,6 @@
1919
import java.io.IOException;
2020
import java.time.Duration;
2121
import java.time.temporal.ChronoUnit;
22-
import java.util.ArrayList;
23-
import java.util.Collections;
2422
import java.util.HashMap;
2523
import java.util.List;
2624
import java.util.Locale;
@@ -69,7 +67,7 @@ public class KafkaProperties {
6967
* List of host:port pairs to use for establishing the initial connections to the
7068
* Kafka cluster. Applies to all components unless overridden.
7169
*/
72-
private List<String> bootstrapServers = new ArrayList<>(Collections.singletonList("localhost:9092"));
70+
private List<String> bootstrapServers = List.of("localhost:9092");
7371

7472
/**
7573
* ID to pass to the server when making requests. Used for server-side logging.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,8 +17,6 @@
1717
package org.springframework.boot.autoconfigure.quartz;
1818

1919
import java.time.Duration;
20-
import java.util.ArrayList;
21-
import java.util.Arrays;
2220
import java.util.HashMap;
2321
import java.util.List;
2422
import java.util.Map;
@@ -155,7 +153,7 @@ public static class Jdbc {
155153
/**
156154
* Prefixes for single-line comments in SQL initialization scripts.
157155
*/
158-
private List<String> commentPrefix = new ArrayList<>(Arrays.asList("#", "--"));
156+
private List<String> commentPrefix = List.of("#", "--");
159157

160158
public String getSchema() {
161159
return this.schema;

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -146,7 +146,7 @@ public List<String> getRoles() {
146146
}
147147

148148
public void setRoles(List<String> roles) {
149-
this.roles = new ArrayList<>(roles);
149+
this.roles = roles;
150150
}
151151

152152
public boolean isPasswordGenerated() {

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/appendix/configurationmetadata/annotationprocessor/automaticmetadatageneration/MyMessagingProperties.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,14 @@
1616

1717
package org.springframework.boot.docs.appendix.configurationmetadata.annotationprocessor.automaticmetadatageneration;
1818

19-
import java.util.ArrayList;
20-
import java.util.Arrays;
2119
import java.util.List;
2220

2321
import org.springframework.boot.context.properties.ConfigurationProperties;
2422

2523
@ConfigurationProperties(prefix = "my.messaging")
2624
public class MyMessagingProperties {
2725

28-
private List<String> addresses = new ArrayList<>(Arrays.asList("a", "b"));
26+
private List<String> addresses = List.of("a", "b");
2927

3028
private ContainerType containerType = ContainerType.SIMPLE;
3129

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/javabeanbinding/MyProperties.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,8 +17,6 @@
1717
package org.springframework.boot.docs.features.externalconfig.typesafeconfigurationproperties.javabeanbinding;
1818

1919
import java.net.InetAddress;
20-
import java.util.ArrayList;
21-
import java.util.Collections;
2220
import java.util.List;
2321

2422
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -60,7 +58,7 @@ public static class Security {
6058

6159
private String password;
6260

63-
private List<String> roles = new ArrayList<>(Collections.singleton("USER"));
61+
private List<String> roles = List.of("USER");
6462

6563
// @fold:on // getters / setters...
6664
public String getUsername() {

0 commit comments

Comments
 (0)