Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: about 1 month 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 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-30T14:57:50.000Z (about 2 months ago)
- Last Synced: 2024-10-06T03:51:40.676Z (about 1 month ago)
- Topics: java, kotlin, spring, spring-boot
- Language: Java
- Homepage:
- Size: 213 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![CI](https://github.com/rogervinas/spring-immutable-configuration-properties/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/rogervinas/spring-immutable-configuration-properties/actions/workflows/ci.yml)
![Java](https://img.shields.io/badge/Java-21-blue?labelColor=black)
![Kotlin](https://img.shields.io/badge/Kotlin-2.0.21-blue?labelColor=black)
![SpringBoot](https://img.shields.io/badge/SpringBoot-3.3.4-blue?labelColor=black)# 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
```