https://github.com/rogervinas/spring-immutable-configuration-properties
🍀 Spring Immutable ConfigurationProperties PoC
https://github.com/rogervinas/spring-immutable-configuration-properties
java kotlin spring spring-boot
Last synced: 3 months ago
JSON representation
🍀 Spring Immutable ConfigurationProperties PoC
- Host: GitHub
- URL: https://github.com/rogervinas/spring-immutable-configuration-properties
- Owner: rogervinas
- Created: 2019-03-27T13:51:04.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-02-03T10:24:48.000Z (5 months ago)
- Last Synced: 2025-02-23T19:37:43.217Z (4 months ago)
- Topics: java, kotlin, spring, spring-boot
- Language: Java
- Homepage:
- Size: 217 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://github.com/rogervinas/spring-immutable-configuration-properties/actions/workflows/ci.yml)


# Spring Immutable @ConfigurationProperties
There are at least 4 ways of having **immutable** properties loaded from configuration:
1) [A plain Java class](src/main/java/com/acme/AcmeJavaClassProperties.java)
```java
@ConfigurationProperties("acme")
public class AcmeJavaClassProperties {private final boolean enabled;
private final String text;
private final List list;
private final float number;public AcmeJavaClassProperties(
boolean enabled,
String text,
List list,
float number
) {
this.enabled = enabled;
this.text = text;
this.list = unmodifiableList(list);
this.number = number;
}
}
```2) [A Java record](src/main/java/com/acme/AcmeJavaRecordProperties.java)
```java
@ConfigurationProperties("acme")
public record AcmeJavaRecordProperties(
boolean enabled,
String text,
List list,
float number
) {
}
```3) [A plain Kotlin class](src/main/java/com/acme/AcmeKotlinClassProperties.kt)
```kotlin
@ConfigurationProperties("acme")
class AcmeKotlinClassProperties(
val enabled: Boolean,
val text: String,
val list: List,
val number: Float
)
```4) [A Kotlin data class](src/main/java/com/acme/AcmeKotlinDataClassProperties.kt)
```kotlin
@ConfigurationProperties("acme")
data class AcmeKotlinDataClassProperties (
val enabled: Boolean,
val text: String,
val list: List,
val number: Float
)
```Note that since Spring Boot 3.x [@ConstructorBinding](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/properties/bind/ConstructorBinding.html) annotation is only required to indicate which constructor to use in case there is more than one.
You can also browse older versions:
* [Spring Boot 2.x](https://github.com/rogervinas/spring-immutable-configuration-properties/tree/spring-boot-2.x)## Test
Review [AcmeApplicationTest](src/test/java/com/acme/AcmeApplicationTest.java) and ...
```bash
./gradlew test
```