File tree 2 files changed +17
-3
lines changed
main/kotlin/org/springframework/context/support
test/kotlin/org/springframework/context/support
2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change 17
17
package org.springframework.context.support
18
18
19
19
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
20
- import org.springframework.context.ApplicationContext
21
20
import org.springframework.core.env.ConfigurableEnvironment
22
21
import java.util.function.Supplier
23
22
@@ -38,12 +37,20 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
38
37
PROTOTYPE
39
38
}
40
39
41
- class BeanDefinitionContext (val context : ApplicationContext ) {
40
+ class BeanDefinitionContext (val context : GenericApplicationContext ) {
42
41
42
+
43
43
inline fun <reified T : Any > ref (name : String? = null) : T = when (name) {
44
44
null -> context.getBean(T ::class .java)
45
45
else -> context.getBean(name, T ::class .java)
46
46
}
47
+
48
+ /* *
49
+ * Get the [ConfigurableEnvironment] associated to the underlying [GenericApplicationContext].
50
+ */
51
+ val env : ConfigurableEnvironment
52
+ get() = context.environment
53
+
47
54
}
48
55
49
56
/* *
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ import org.junit.Test
21
21
import org.springframework.beans.factory.NoSuchBeanDefinitionException
22
22
import org.springframework.beans.factory.getBean
23
23
import org.springframework.context.support.BeanDefinitionDsl.*
24
+ import org.springframework.core.env.SimpleCommandLinePropertySource
24
25
25
26
class BeanDefinitionDslTests {
26
27
@@ -75,13 +76,16 @@ class BeanDefinitionDslTests {
75
76
val beans = beans {
76
77
bean<Foo >()
77
78
bean<Bar >(" bar" )
79
+ bean { FooFoo (it.env.getProperty(" name" )) }
78
80
environment({it.activeProfiles.contains(" baz" )}) {
79
81
bean { Baz (it.ref()) }
80
82
bean { Baz (it.ref(" bar" )) }
81
83
}
82
84
}
83
85
84
- val context = GenericApplicationContext ()
86
+ val context = GenericApplicationContext ().apply {
87
+ environment.propertySources.addFirst(SimpleCommandLinePropertySource (" --name=foofoo" ))
88
+ }
85
89
beans.invoke(context)
86
90
context.refresh()
87
91
@@ -92,10 +96,13 @@ class BeanDefinitionDslTests {
92
96
fail(" Expect NoSuchBeanDefinitionException to be thrown" )
93
97
}
94
98
catch (ex: NoSuchBeanDefinitionException ) { null }
99
+ val foofoo = context.getBean<FooFoo >()
100
+ assertEquals(" foofoo" , foofoo.name)
95
101
}
96
102
97
103
}
98
104
99
105
class Foo
100
106
class Bar
101
107
class Baz (val bar : Bar )
108
+ class FooFoo (val name : String )
You can’t perform that action at this time.
0 commit comments