Library containing reusable constants for Spring and Spring Boot
- Java 21
- Spring Boot 3
implementation("io.opengood.constants:spring-constants:VERSION")
<dependency>
<groupId>io.opengood.constants</groupId>
<artifactId>spring-constants</artifactId>
<version>VERSION</version>
</dependency>
Note: See Release version badge above for latest version.
Note: All examples are provided in Kotlin
Common Spring properties are often referenced in code for importing configuration values. Rather than defining these constantly, simply refer to them as constants.
Constant | Spring Property |
---|---|
SpringProperties.APPLICATION_NAME |
spring.application.name |
SpringProperties.DATA_MONGODB_DATABASE |
spring.data.mongodb.database |
SpringProperties.DATA_MONGODB_HOST |
spring.data.mongodb.host |
SpringProperties.DATA_MONGODB_PASSWORD |
spring.data.mongodb.password |
SpringProperties.DATA_MONGODB_PORT |
spring.data.mongodb.port |
SpringProperties.DATA_MONGODB_URI |
spring.data.mongodb.uri |
SpringProperties.DATA_MONGODB_USERNAME |
spring.data.mongodb.username |
Similarly, when using @Value
to import Spring property values, one needs to
wrap ${}
around the property. These are also provided as constants one can
simply refer.
Constant | Spring Property Placeholder |
---|---|
SpringPropertyPlaceholders.APPLICATION_NAME |
${spring.application.name} |
SpringPropertyPlaceholders.DATA_MONGODB_DATABASE |
${spring.data.mongodb.database} |
SpringPropertyPlaceholders.DATA_MONGODB_HOST |
${spring.data.mongodb.host} |
SpringPropertyPlaceholders.DATA_MONGODB_PASSWORD |
${spring.data.mongodb.password} |
SpringPropertyPlaceholders.DATA_MONGODB_PORT |
${spring.data.mongodb.port} |
SpringPropertyPlaceholders.DATA_MONGODB_URI |
${spring.data.mongodb.uri} |
SpringPropertyPlaceholders.DATA_MONGODB_USERNAME |
${spring.data.mongodb.username} |
Example:
import io.opengood.constants.spring.SpringPropertyPlaceholders
@Configuration
class AppConfig {
@Bean
fun bean(@Value(SpringPropertyPlaceholders.APPLICATION_NAME) value: String): String {
// configure bean
}
}
Sometimes one needs to use defined Spring property settings and remembering a specific property is hard. Constants are provided to simplify this:
Constant | Spring Property Setting |
---|---|
SpringPropertySettings.BEAN_DEFINITION_OVERRIDE |
spring.main.allow-bean-definition-overriding=true |
Example:
import io.opengood.constants.spring.SpringPropertySettings
@SpringBootTest(
classes = [TestApplication::class],
properties = [SpringPropertySettings.BEAN_DEFINITION_OVERRIDE],
webEnvironment = WebEnvironment.RANDOM_PORT
)
class ControllerTest : WordSpec() {
// do stuff
}