Skip to content

Commit d2c1b28

Browse files
committed
Polish Kotlin API contracts and documentation
Issue: SPR-15659
1 parent 3f2e27f commit d2c1b28

File tree

3 files changed

+361
-62
lines changed

3 files changed

+361
-62
lines changed

spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt

Lines changed: 95 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,99 @@ import org.springframework.beans.factory.config.BeanDefinitionCustomizer
2020
import org.springframework.core.env.ConfigurableEnvironment
2121
import java.util.function.Supplier
2222

23+
/**
24+
* Functional bean definition Kotlin DSL.
25+
*
26+
* Example:
27+
*
28+
* ```
29+
* beans {
30+
* bean<UserHandler>()
31+
* bean {
32+
* Routes(ref(), ref())
33+
* }
34+
* bean<WebHandler>("webHandler") {
35+
* RouterFunctions.toWebHandler(
36+
* ref<Routes>().router(),
37+
* HandlerStrategies.builder().viewResolver(ref()).build())
38+
* }
39+
* bean("messageSource") {
40+
* ReloadableResourceBundleMessageSource().apply {
41+
* setBasename("messages")
42+
* setDefaultEncoding("UTF-8")
43+
* }
44+
* }
45+
* bean {
46+
* val prefix = "classpath:/templates/"
47+
* val suffix = ".mustache"
48+
* val loader = MustacheResourceTemplateLoader(prefix, suffix)
49+
* MustacheViewResolver(Mustache.compiler().withLoader(loader)).apply {
50+
* setPrefix(prefix)
51+
* setSuffix(suffix)
52+
* }
53+
* }
54+
* profile("foo") {
55+
* bean<Foo>()
56+
* }
57+
* }
58+
* ```
59+
*
60+
* @author Sebastien Deleuze
61+
* @see BeanDefinitionDsl
62+
* @since 5.0
63+
*/
64+
fun beans(init: BeanDefinitionDsl.() -> Unit): BeanDefinitionDsl {
65+
val beans = BeanDefinitionDsl()
66+
beans.init()
67+
return beans
68+
}
69+
2370
/**
2471
* Class implementing functional bean definition Kotlin DSL.
2572
*
73+
* @constructor Create a new bean definition DSL.
74+
* @param condition the predicate to fulfill in order to take in account the inner bean definition block
2675
* @author Sebastien Deleuze
2776
* @since 5.0
2877
*/
29-
open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean = { true }) : (GenericApplicationContext) -> Unit {
78+
class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) -> Boolean = { true }) : (GenericApplicationContext) -> Unit {
3079

31-
protected val registrations = arrayListOf<(GenericApplicationContext) -> Unit>()
32-
33-
protected val children = arrayListOf<BeanDefinitionDsl>()
34-
80+
@PublishedApi
81+
internal val registrations = arrayListOf<(GenericApplicationContext) -> Unit>()
82+
83+
@PublishedApi
84+
internal val children = arrayListOf<BeanDefinitionDsl>()
85+
86+
/**
87+
* Scope enum constants.
88+
*/
3589
enum class Scope {
90+
/**
91+
* Scope constant for the standard singleton scope
92+
* @see org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON
93+
*/
3694
SINGLETON,
95+
/**
96+
* Scope constant for the standard singleton scope
97+
* @see org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE
98+
*/
3799
PROTOTYPE
38100
}
39101

40-
class BeanDefinitionContext(val context: GenericApplicationContext) {
102+
/**
103+
* Provide read access to some application context facilities.
104+
* @constructor Create a new bean definition context.
105+
* @param context the `ApplicationContext` instance to use for retrieving bean references, `Environment`, etc.
106+
*/
107+
inner class BeanDefinitionContext(@PublishedApi internal val context: GenericApplicationContext) {
41108

42-
109+
/**
110+
* Get a reference to the bean by type or type + name with the syntax
111+
* `ref<Foo>()` or `ref<Foo>("foo")`. When leveraging Kotlin type inference
112+
* it could be as short as `ref()` or `ref("foo")`.
113+
* @param name the name of the bean to retrieve
114+
* @param T type the bean must match, can be an interface or superclass
115+
*/
43116
inline fun <reified T : Any> ref(name: String? = null) : T = when (name) {
44117
null -> context.getBean(T::class.java)
45118
else -> context.getBean(name, T::class.java)
@@ -55,8 +128,14 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
55128

56129
/**
57130
* Declare a bean definition from the given bean class which can be inferred when possible.
58-
*
59-
* @See GenericApplicationContext.registerBean
131+
*
132+
* @param name the name of the bean
133+
* @param scope Override the target scope of this bean, specifying a new scope name.
134+
* @param isLazyInit Set whether this bean should be lazily initialized.
135+
* @param isPrimary Set whether this bean is a primary autowire candidate.
136+
* @param isAutowireCandidate Set whether this bean is a candidate for getting autowired into some other bean.
137+
* @see GenericApplicationContext.registerBean
138+
* @see org.springframework.beans.factory.config.BeanDefinition
60139
*/
61140
inline fun <reified T : Any> bean(name: String? = null,
62141
scope: Scope? = null,
@@ -82,7 +161,7 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
82161
/**
83162
* Declare a bean definition using the given supplier for obtaining a new instance.
84163
*
85-
* @See GenericApplicationContext.registerBean
164+
* @see GenericApplicationContext.registerBean
86165
*/
87166
inline fun <reified T : Any> bean(name: String? = null,
88167
scope: Scope? = null,
@@ -97,7 +176,7 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
97176
isPrimary?.let { bd.isPrimary = isPrimary }
98177
isAutowireCandidate?.let { bd.isAutowireCandidate = isAutowireCandidate }
99178
}
100-
179+
101180
registrations.add {
102181
val beanContext = BeanDefinitionContext(it)
103182
when (name) {
@@ -121,6 +200,7 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
121200
/**
122201
* Take in account bean definitions enclosed in the provided lambda only when the
123202
* specified environment-based predicate is true.
203+
* @param condition the predicate to fulfill in order to take in account the inner bean definition block
124204
*/
125205
fun environment(condition: ConfigurableEnvironment.() -> Boolean, init: BeanDefinitionDsl.() -> Unit): BeanDefinitionDsl {
126206
val beans = BeanDefinitionDsl(condition::invoke)
@@ -129,6 +209,10 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
129209
return beans
130210
}
131211

212+
/**
213+
* Register the bean defined via the DSL on thAh pe provided application context.
214+
* @param context The `ApplicationContext` to use for registering the beans
215+
*/
132216
override fun invoke(context: GenericApplicationContext) {
133217
for (registration in registrations) {
134218
if (condition.invoke(context.environment)) {
@@ -140,15 +224,3 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
140224
}
141225
}
142226
}
143-
144-
/**
145-
* Functional bean definition Kotlin DSL.
146-
*
147-
* @author Sebastien Deleuze
148-
* @since 5.0
149-
*/
150-
fun beans(init: BeanDefinitionDsl.() -> Unit): BeanDefinitionDsl {
151-
val beans = BeanDefinitionDsl()
152-
beans.init()
153-
return beans
154-
}

spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import reactor.core.publisher.Mono
2323

2424

2525
/**
26-
* Extension for [WebClient.RequestBodySpec.body] providing a `body<Foo>() variant
26+
* Extension for [WebClient.RequestBodySpec.body] providing a `body<Foo>()` variant
2727
* leveraging Kotlin reified type parameters.
2828
*
2929
* @author Sebastien Deleuze

0 commit comments

Comments
 (0)