Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document it is possible to use Java Records for immutable configuration with @ConfigurationProperties #10096

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/docs/guide/config/immutableConfig.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ snippet::io.micronaut.docs.config.immutable.VehicleSpec[tags="start",indent=0,ti

The above example prints: `"Ford Engine Starting V8 [rodLength=7B.0]"`

==== Using Java Record Classes to define immutable configuration

For Java language applications, it's also possible to use https://docs.oracle.com/en/java/javase/17/language/records.html[Java Record Classes] for immutable configuration with ann:context.annotation.ConfigurationProperties[]. For example:

snippet::io.micronaut.docs.config.immutable.ValueAddedTaxConfiguration[tags="imports,class",title="Java Record Example"]

<1> The `percentage` field defines a configuration property for "vat"

NOTE: From a performance perspective Java records are better than interfaces.

== Customizing accessors

As already explained in <<configurationPropertiesAccessorsStyle, Change accessors style>>, it is also possible to customize the accessors when creating immutable configuration properties:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.micronaut.docs.config.immutable;

// tag::imports[]
import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.NonNull;
import jakarta.validation.constraints.NotNull;
import java.math.BigDecimal;
// end::imports[]

@Requires(property="spec.name", value="ValueAddedTaxConfigurationTest")
// tag::class[]
@ConfigurationProperties("vat")
public record ValueAddedTaxConfiguration(
@NonNull @NotNull BigDecimal percentage) { // <1>
}
// end::class[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.micronaut.docs.config.immutable;

import io.micronaut.context.BeanContext;
import io.micronaut.context.annotation.Property;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

@Property(name = "spec.name", value="ValueAddedTaxConfigurationTest")
@Property(name = "vat.percentage", value = "21.0")
@MicronautTest(startApplication = false)
class ValueAddedTaxConfigurationTest {

@Inject
BeanContext beanContext;

@Test
void immutableConfigurationViaJavaRecords() {
assertTrue(beanContext.containsBean(ValueAddedTaxConfiguration.class));
assertEquals(new BigDecimal("21.0"), beanContext.getBean(ValueAddedTaxConfiguration.class).percentage());
}
}
Loading