| 
         此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10!  | 
    
| 
         此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10!  | 
    
Spring Framework 支持使用 lambda 以函数方式注册 Bean
作为 XML 或 Java 配置( 和 )的替代方法。简而言之
它允许您使用充当 .
这种机制非常有效,因为它不需要任何反射或 CGLIB 代理。@Configuration@BeanFactoryBean
例如,在 Java 中,您可以编写以下内容:
class Foo {}
class Bar {
	private final Foo foo;
	public Bar(Foo foo) {
		this.foo = foo;
	}
}
GenericApplicationContext context = new GenericApplicationContext();
context.registerBean(Foo.class);
context.registerBean(Bar.class, () -> new Bar(context.getBean(Foo.class)));
在 Kotlin 中,使用具体化类型参数和 Kotlin 扩展,
您可以改为编写以下内容:GenericApplicationContext
class Foo
class Bar(private val foo: Foo)
val context = GenericApplicationContext().apply {
	registerBean<Foo>()
	registerBean { Bar(it.getBean()) }
}
当类具有单个构造函数时,您甚至可以只指定 Bean 类,
构造函数参数将按类型自动连线:Bar
val context = GenericApplicationContext().apply {
	registerBean<Foo>()
	registerBean<Bar>()
}
为了允许更声明的方法和更简洁的语法,Spring Framework 提供了
一个 Kotlin Bean 定义 DSL 它通过一个干净的声明式 API 声明一个,
这使您可以处理配置文件和自定义
Bean 是如何注册的。ApplicationContextInitializerEnvironment
在以下示例中,请注意:
- 
类型推断通常允许避免指定 Bean 引用的类型,例如
ref("bazBean") - 
可以使用 Kotlin 顶级函数通过可调用引用来声明 Bean,如下例所示
bean(::myRouter) - 
指定 或 时,参数按类型自动连线
bean<Bar>()bean(::myRouter) - 
仅当概要文件处于活动状态时,才会注册 Bean
FooBarfoobar 
class Foo
class Bar(private val foo: Foo)
class Baz(var message: String = "")
class FooBar(private val baz: Baz)
val myBeans = beans {
	bean<Foo>()
	bean<Bar>()
	bean("bazBean") {
		Baz().apply {
			message = "Hello world"
		}
	}
	profile("foobar") {
		bean { FooBar(ref("bazBean")) }
	}
	bean(::myRouter)
}
fun myRouter(foo: Foo, bar: Bar, baz: Baz) = router {
	// ...
}
此 DSL 是编程的,这意味着它允许 Bean 的自定义注册逻辑
通过表达式、循环或任何其他 Kotlin 构造。iffor | 
此 DSL 是编程的,这意味着它允许 Bean 的自定义注册逻辑
通过表达式、循环或任何其他 Kotlin 构造。iffor | 
然后,您可以使用此函数在应用程序上下文中注册 Bean,
如以下示例所示:beans()
val context = GenericApplicationContext().apply {
	myBeans.initialize(this)
	refresh()
}
Spring Boot 基于 JavaConfig,尚未提供对函数式 Bean 定义的特定支持,
但是你可以通过Spring Boot的支持来实验性地使用功能Bean定义。
有关更多详细信息和最新信息,请参阅此 Stack Overflow 答案。另请参阅在Spring Fu孵化器中开发的实验性Kofu DSL。ApplicationContextInitializer | 
Spring Boot 基于 JavaConfig,尚未提供对函数式 Bean 定义的特定支持,
但是你可以通过Spring Boot的支持来实验性地使用功能Bean定义。
有关更多详细信息和最新信息,请参阅此 Stack Overflow 答案。另请参阅在Spring Fu孵化器中开发的实验性Kofu DSL。ApplicationContextInitializer |