Call functions directly in your Spring Boot application.yaml:
release-version: "${fn.firstNonEmpty(${git.tags:}, ${git.commit.id.abbrev:}, unknown)}"Add the dependency to your pom.xml:
<dependency>
<groupId>team.dedica</groupId>
<artifactId>config-functions</artifactId>
<version>1.2.0</version>
</dependency>That's it. The library is automatically activated via Spring Boot's EnvironmentPostProcessor mechanism.
Function calls are prefixed with fn. and can be used like any other property placeholder:
my-property: "${fn.functionName(arguments)}"You can pass other Spring properties as arguments:
my-property: "${fn.functionName(${spring.application.name})}"Returns the first non-blank value from a comma-separated list of arguments.
my-property: "${fn.firstNonEmpty(,a,b)}" # returns "a"This is useful for defining fallback chains:
release-version: "${fn.firstNonEmpty(${git.tags:}, ${git.commit.id.abbrev:}, unknown)}"In this example, the release version is resolved from the following sources in order:
- The Git tag (if available)
- The abbreviated commit hash (if available)
- The literal string
unknown
Always use the : suffix for properties that may not exist:
# Good - empty string fallback if the property is missing
"${fn.firstNonEmpty(${my.optional.property:}, fallback)}"
# Bad - if the property is missing, the literal string "${my.optional.property}" is used,
# which is non-empty and will be returned as-is
"${fn.firstNonEmpty(${my.optional.property}, fallback)}"Checks that all property placeholders in the argument have been resolved. If any unresolved placeholders are detected, an exception is thrown at startup.
db-password: "${fn.requireResolved(${DB_PASSWORD})}"If DB_PASSWORD is set, the resolved value is returned. If it is not set, the function will throw an error:
The following variables have not been resolved: ${DB_PASSWORD}
Multiple variables can be checked at once:
jdbc-url: "${fn.requireResolved(jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME})}"If any of them are unresolved, all unresolved variables are listed in the error message.
Decodes a Base64-encoded argument and returns the result as string.
This is useful, when configuration values are passed as Base64-encoded strings, for example SSL certificates or keys provided via environment variables:
ssl-certificate: "${fn.base64Decode(${SSL_CERT_BASE64})}"Encodes the argument for safe use in URLs.
This is useful, when composing URLs from properties that may contain special characters
such as @, #, or %:
jdbc-url: "jdbc:postgresql://user:${fn.urlEncode(${DB_PASSWORD})}@localhost:5432/mydb"- Spring Boot 3.x
- Java 17+