Skip to content

Library containing reusable constants for Spring and Spring Boot

License

Notifications You must be signed in to change notification settings

opengood-aio/spring-constants

Repository files navigation

Spring Constants Library

Build Release CodeQL Codecov Release Version Maven Central License

Library containing reusable constants for Spring and Spring Boot

Compatibility

  • Java 21
  • Spring Boot 3

Setup

Add Dependency

Gradle

implementation("io.opengood.constants:spring-constants:VERSION")

Maven

<dependency>
    <groupId>io.opengood.constants</groupId>
    <artifactId>spring-constants</artifactId>
    <version>VERSION</version>
</dependency>

Note: See Release version badge above for latest version.

Features

Note: All examples are provided in Kotlin

Reusable Spring Properties

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

Reusable Spring Property Placeholders

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
    }
}

Reusable Spring Property Settings

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
}