From 3f1882c4eb2ec7a749555d0b7ef05e1262e35447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 13 Jan 2020 10:51:19 +0100 Subject: [PATCH] Support noarg callable references in Kotlin beans DSL Closes gh-23395 --- .../context/support/BeanDefinitionDsl.kt | 36 +++++++++++++++++++ .../context/support/BeanDefinitionDslTests.kt | 4 ++- 2 files changed, 39 insertions(+), 1 deletion(-) 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 19bf94c0384c..a2c9773e2335 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 @@ -236,6 +236,42 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit context.registerBean(beanName, T::class.java, Supplier { function.invoke(BeanSupplierContext(context)) }, customizer) } + /** + * Declare a bean definition using the given callable reference with no parameter + * for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2.3 + */ + inline fun + bean(crossinline f: () -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke() + } + } + /** * Declare a bean definition using the given callable reference with 1 parameter * autowired by type for obtaining a new instance. 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 cc315fcddecd..5ac22c8d64f6 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -156,6 +156,7 @@ class BeanDefinitionDslTests { val beans = beans { bean() bean(::baz) + bean(::foo) } val context = GenericApplicationContext().apply { beans.initialize(this) @@ -205,3 +206,4 @@ class FooFoo(val name: String) class BarBar(val foos: Collection) fun baz(bar: Bar) = Baz(bar) +fun foo() = Foo()