From 29c112c0102300b687dd7803f5c5866175ddb644 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Tue, 25 Jul 2017 11:18:52 +0200 Subject: [PATCH] Avoid requiring `it` parameter in Kotlin bean DSL By using function literals with receiver, we can avoid requiring lambda parameters for a shorter and nicer syntax. Based on a proposal from Joseph Taylor. Issue: SPR-15815 --- .../context/support/BeanDefinitionDsl.kt | 4 ++-- .../context/support/BeanDefinitionDslTests.kt | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt index 4ddbc9ab05..24006d41a6 100644 --- a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt +++ b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt @@ -89,7 +89,7 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean isLazyInit: Boolean? = null, isPrimary: Boolean? = null, isAutowireCandidate: Boolean? = null, - crossinline function: (BeanDefinitionContext) -> T) { + crossinline function: BeanDefinitionContext.() -> T) { val customizer = BeanDefinitionCustomizer { bd -> scope?.let { bd.scope = scope.name.toLowerCase() } @@ -122,7 +122,7 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean * Take in account bean definitions enclosed in the provided lambda only when the * specified environment-based predicate is true. */ - fun environment(condition: (ConfigurableEnvironment) -> Boolean, init: BeanDefinitionDsl.() -> Unit): BeanDefinitionDsl { + fun environment(condition: ConfigurableEnvironment.() -> Boolean, init: BeanDefinitionDsl.() -> Unit): BeanDefinitionDsl { val beans = BeanDefinitionDsl(condition::invoke) beans.init() children.add(beans) diff --git a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt index 2d3ab5dd92..53923231b1 100644 --- a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt @@ -32,8 +32,8 @@ class BeanDefinitionDslTests { val beans = beans { bean() bean("bar", scope = Scope.PROTOTYPE) - bean { Baz(it.ref()) } - bean { Baz(it.ref("bar")) } + bean { Baz(ref()) } + bean { Baz(ref("bar")) } } val context = GenericApplicationContext() @@ -55,8 +55,8 @@ class BeanDefinitionDslTests { profile("pp") { bean() } - bean { Baz(it.ref()) } - bean { Baz(it.ref("bar")) } + bean { Baz(ref()) } + bean { Baz(ref("bar")) } } } @@ -78,10 +78,10 @@ class BeanDefinitionDslTests { val beans = beans { bean() bean("bar") - bean { FooFoo(it.env["name"]) } - environment({it.activeProfiles.contains("baz")}) { - bean { Baz(it.ref()) } - bean { Baz(it.ref("bar")) } + bean { FooFoo(env["name"]) } + environment( { activeProfiles.contains("baz") } ) { + bean { Baz(ref()) } + bean { Baz(ref("bar")) } } }