Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.theValueAnnotation

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.PropertySource


@PropertySource(value = ["classpath:application-inject-value.yml"], factory = YamlPropertySourceFactory::class)
@SpringBootApplication(scanBasePackages = ["com.baeldung.theValueAnnotation"])
class KotlinValueInjectionApplication

fun main(args: Array<String>) {
SpringApplication.run(KotlinValueInjectionApplication::class.java, *args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.theValueAnnotation

import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

@Component
class ValueBean(
// without default values
@Value("\${my-app.magic-number}")
val magicNumber: Int,
@Value("\${my-app.magic-string}")
val magicString: String,
@Value("\${my-app.magic-flag}")
val magicFlag: Boolean,

// with default values
@Value("\${my-app.not-defined-value:1024}")
val magicNumberWithDefault: Int,
@Value("\${my-app.magic-string-with-default:default Value}")
val magicStringWithDefault: String,

// with null as the default value
@Value("\${my-app.not-defined-value:null}")
val stringDefaultLiteralNull: String?,
@Value("\${my-app.not-defined-value:#{null}}")
val stringDefaultNull: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.theValueAnnotation

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean
import org.springframework.core.env.PropertiesPropertySource
import org.springframework.core.env.PropertySource
import org.springframework.core.io.support.EncodedResource
import org.springframework.core.io.support.PropertySourceFactory
import org.springframework.lang.Nullable

class YamlPropertySourceFactory : PropertySourceFactory {
override fun createPropertySource(@Nullable name: String?, encodedResource: EncodedResource): PropertySource<*> =
PropertiesPropertySource(
encodedResource.resource.filename,
(YamlPropertiesFactoryBean().also { it.setResources(encodedResource.resource) }.getObject())
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
my-app:
magic-number: 42
magic-string: "It's a magic string"
magic-flag: true

magic-string-with-default: "It's another magic string"
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baeldung.theValueAnnotation

import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestConstructor
import org.springframework.test.context.TestConstructor.AutowireMode
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

@SpringBootTest
@TestConstructor(autowireMode = AutowireMode.ALL)
class TheValueAnnotationUnitTest(val valueBean: ValueBean) {

@Test
fun `when using value annotations, then expected values should be injected`() {
with(valueBean) {
assertEquals(42, magicNumber)
assertEquals("It's a magic string", magicString)
assertTrue(magicFlag)
}
}

@Test
fun `when using value annotations with default values, then expected values should be injected`() {
with(valueBean) {
assertEquals(1024, magicNumberWithDefault)
assertEquals("It's another magic string", magicStringWithDefault)
}
}

@Test
fun `when using value annotations with null as default values, then expected values should be injected`() {
with(valueBean) {
assertEquals("null", stringDefaultLiteralNull)
assertNull(stringDefaultNull)
}
}
}