This project demonstrates how to use Java records with Spring Boot to define custom configuration properties. These properties include validations and metadata annotations to enhance IDE support and ensure data integrity.
Java records, introduced in Java 14, provide a concise and readable way to define immutable data objects. In this project, we utilize Java records to define our custom configuration properties. Each record represents a set of related configuration properties with predefined fields.
Validation constraints from the jakarta.validation.constraints
package are applied to the fields of the Java records. These constraints ensure that the configuration properties meet certain criteria, such as being not blank or positive. By enforcing validations, we can prevent invalid configurations and maintain data integrity within our application.
In addition of Javadoc, metadata annotations such as @DefaultValue
are used to provide default values for configuration properties and enhance IDE support. These annotations improve the developer experience by providing additional context and information about the properties directly in the code.
- Define your custom configuration properties using Java records in the specified package.
- Apply validation constraints to the fields of the records to ensure data integrity.
- Use metadata annotations to provide default values and enhance IDE support.
- Integrate the custom configuration properties into your Spring Boot application by specifying the prefix and binding them from the
application.yml
orapplication.properties
file. - Import configuration properties classes explicility or by package scanning in the application main method.
app:
buffer-max-size: 500
bulk-size: 100
collection-name: address
enriching-key: product
enriching-uri: http://localhost:1519/products/1
- Spring Boot
- Jakarta Validation API
- https://medium.com/@boysbee/unable-to-load-io-netty-resolver-dns-macos-macosdnsserveraddressstreamprovider-46d89bf74d42
- https://spring.io/blog/2023/10/31/what-new-is-coming-in-reactor-core-3-6-0#virtual-threads-support
- https://dzone.com/articles/two-ways-configuration-documentation-with-springnb
- https://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html#appendix.configuration-metadata.annotation-processor
- https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix.configuration-metadata.annotation-processor
- https://www.baeldung.com/configuration-properties-in-spring-boot
- https://mydeveloperplanet.com/2023/10/04/how-to-generate-spring-properties-documentation/