File tree Expand file tree Collapse file tree 5 files changed +102
-0
lines changed
kotlin/com/baeldung/theValueAnnotation
test/kotlin/com/baeldung/theValueAnnotation Expand file tree Collapse file tree 5 files changed +102
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments