Skip to content

Commit 4a839ab

Browse files
authored
Merge pull request #806 from sk1418/sb-@value
[sb-@value] @value inject in Kotlin
2 parents 68a8b55 + 74351e1 commit 4a839ab

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.theValueAnnotation
2+
3+
import org.springframework.boot.SpringApplication
4+
import org.springframework.boot.autoconfigure.SpringBootApplication
5+
import org.springframework.context.annotation.PropertySource
6+
7+
8+
@PropertySource(value = ["classpath:application-inject-value.yml"], factory = YamlPropertySourceFactory::class)
9+
@SpringBootApplication(scanBasePackages = ["com.baeldung.theValueAnnotation"])
10+
class KotlinValueInjectionApplication
11+
12+
fun main(args: Array<String>) {
13+
SpringApplication.run(KotlinValueInjectionApplication::class.java, *args)
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.theValueAnnotation
2+
3+
import org.springframework.beans.factory.annotation.Value
4+
import org.springframework.stereotype.Component
5+
6+
@Component
7+
class ValueBean(
8+
// without default values
9+
@Value("\${my-app.magic-number}")
10+
val magicNumber: Int,
11+
@Value("\${my-app.magic-string}")
12+
val magicString: String,
13+
@Value("\${my-app.magic-flag}")
14+
val magicFlag: Boolean,
15+
16+
// with default values
17+
@Value("\${my-app.not-defined-value:1024}")
18+
val magicNumberWithDefault: Int,
19+
@Value("\${my-app.magic-string-with-default:default Value}")
20+
val magicStringWithDefault: String,
21+
22+
// with null as the default value
23+
@Value("\${my-app.not-defined-value:null}")
24+
val stringDefaultLiteralNull: String?,
25+
@Value("\${my-app.not-defined-value:#{null}}")
26+
val stringDefaultNull: String?
27+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.theValueAnnotation
2+
3+
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean
4+
import org.springframework.core.env.PropertiesPropertySource
5+
import org.springframework.core.env.PropertySource
6+
import org.springframework.core.io.support.EncodedResource
7+
import org.springframework.core.io.support.PropertySourceFactory
8+
import org.springframework.lang.Nullable
9+
10+
class YamlPropertySourceFactory : PropertySourceFactory {
11+
override fun createPropertySource(@Nullable name: String?, encodedResource: EncodedResource): PropertySource<*> =
12+
PropertiesPropertySource(
13+
encodedResource.resource.filename,
14+
(YamlPropertiesFactoryBean().also { it.setResources(encodedResource.resource) }.getObject())
15+
)
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
my-app:
2+
magic-number: 42
3+
magic-string: "It's a magic string"
4+
magic-flag: true
5+
6+
magic-string-with-default: "It's another magic string"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.theValueAnnotation
2+
3+
import org.junit.jupiter.api.Test
4+
import org.springframework.boot.test.context.SpringBootTest
5+
import org.springframework.test.context.TestConstructor
6+
import org.springframework.test.context.TestConstructor.AutowireMode
7+
import kotlin.test.assertEquals
8+
import kotlin.test.assertNull
9+
import kotlin.test.assertTrue
10+
11+
@SpringBootTest
12+
@TestConstructor(autowireMode = AutowireMode.ALL)
13+
class TheValueAnnotationUnitTest(val valueBean: ValueBean) {
14+
15+
@Test
16+
fun `when using value annotations, then expected values should be injected`() {
17+
with(valueBean) {
18+
assertEquals(42, magicNumber)
19+
assertEquals("It's a magic string", magicString)
20+
assertTrue(magicFlag)
21+
}
22+
}
23+
24+
@Test
25+
fun `when using value annotations with default values, then expected values should be injected`() {
26+
with(valueBean) {
27+
assertEquals(1024, magicNumberWithDefault)
28+
assertEquals("It's another magic string", magicStringWithDefault)
29+
}
30+
}
31+
32+
@Test
33+
fun `when using value annotations with null as default values, then expected values should be injected`() {
34+
with(valueBean) {
35+
assertEquals("null", stringDefaultLiteralNull)
36+
assertNull(stringDefaultNull)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)