Skip to content

🍀 Spring Immutable ConfigurationProperties PoC

Notifications You must be signed in to change notification settings

rogervinas/spring-immutable-configuration-properties

Repository files navigation

CI Java Kotlin SpringBoot

Spring Immutable @ConfigurationProperties

There are at least 4 ways of having immutable properties loaded from configuration:

  1. A plain Java class
@ConfigurationProperties("acme")
public class AcmeJavaClassProperties {

  private final boolean enabled;
  private final String text;
  private final List<String> list;
  private final float number;

  public AcmeJavaClassProperties(
    boolean enabled,
    String text,
    List<String> list,
    float number
  ) {
    this.enabled = enabled;
    this.text = text;
    this.list = unmodifiableList(list);
    this.number = number;
    }
}
  1. A Java record
@ConfigurationProperties("acme")
public record AcmeJavaRecordProperties(
  boolean enabled,
  String text,
  List<String> list,
  float number
) {
}
  1. A plain Kotlin class
@ConfigurationProperties("acme")
class AcmeKotlinClassProperties(
  val enabled: Boolean,
  val text: String,
  val list: List<String>,
  val number: Float
)
  1. A Kotlin data class
@ConfigurationProperties("acme")
data class AcmeKotlinDataClassProperties (
  val enabled: Boolean,
  val text: String,
  val list: List<String>,
  val number: Float
)

Note that since Spring Boot 3.x @ConstructorBinding annotation is only required to indicate which constructor to use in case there is more than one.

You can also browse older versions:

Test

Review AcmeApplicationTest and ...

./gradlew test