Skip to content

Commit 1f7c46e

Browse files
authored
[KTLN-825] Add samples (#909)
1 parent 59c498e commit 1f7c46e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.value
2+
3+
import org.springframework.beans.factory.annotation.Value
4+
import org.springframework.stereotype.Component
5+
6+
@Component
7+
class MyBean {
8+
@Value("\${some.property}")
9+
lateinit var propertyValue: String
10+
}
11+
12+
@Component
13+
class MyBean2(@Value("\${some.property}") val propertyValue: String)
14+
15+
@Component
16+
class MyBean3(@Value("\${non-existent-property:spring-default}") val propertyValue: String)
17+
18+
@Component
19+
class MyBean4(@Value("\${non-existent-property:#{null}}") val propertyValue: String?)

spring-boot-kotlin-2/src/main/resources/application.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ spring:
88
username: sa
99
password:
1010
driver-class-name: org.hsqldb.jdbcDriver
11+
12+
some.property: "abc"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.value
2+
3+
import com.baeldung.consoleapp.CommandLineFirstComponent
4+
import org.assertj.core.api.Assertions.assertThat
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.springframework.beans.factory.annotation.Autowired
8+
import org.springframework.boot.test.context.SpringBootTest
9+
import org.springframework.test.context.junit4.SpringRunner
10+
import kotlin.test.assertEquals
11+
12+
@RunWith(SpringRunner::class)
13+
@SpringBootTest
14+
class ValueAnnotationIntegrationTest {
15+
16+
@Autowired
17+
lateinit var myBean: MyBean
18+
19+
@Autowired
20+
lateinit var myBean2: MyBean2
21+
22+
@Autowired
23+
lateinit var myBean3: MyBean3
24+
25+
@Autowired
26+
lateinit var myBean4: MyBean4
27+
28+
29+
@Test
30+
fun `Verify beans are getting the correct value`() {
31+
assertEquals("abc", myBean.propertyValue)
32+
assertEquals("abc", myBean2.propertyValue)
33+
assertEquals("spring-default", myBean3.propertyValue)
34+
assertEquals(null, myBean4.propertyValue)
35+
}
36+
37+
}

0 commit comments

Comments
 (0)