From 8d951c569b2e3ba56f36b42962e74ff81fd8628b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 27 Oct 2025 12:05:40 +0100 Subject: [PATCH] Document BeanRegistrarDsl with RouterFunctionDsl Closes gh-35549 --- framework-docs/modules/ROOT/nav.adoc | 2 +- .../java/programmatic-bean-registration.adoc | 67 +------------------ ...on-dsl.adoc => bean-registration-dsl.adoc} | 4 +- .../MyBeanRegistrar.java | 53 +++++++++++++++ .../MyConfiguration.java | 27 ++++++++ .../beansjavaprogrammaticregistration/Bar.kt | 19 ++++++ .../beansjavaprogrammaticregistration/Baz.kt | 19 ++++++ .../beansjavaprogrammaticregistration/Foo.kt | 19 ++++++ .../MyBeanRegistrar.kt | 42 ++++++++++++ .../MyConfiguration.kt | 27 ++++++++ .../MyRepository.kt | 20 ++++++ 11 files changed, 231 insertions(+), 68 deletions(-) rename framework-docs/modules/ROOT/pages/languages/kotlin/{bean-definition-dsl.adoc => bean-registration-dsl.adoc} (62%) create mode 100644 framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.java create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Bar.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Baz.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Foo.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyRepository.kt diff --git a/framework-docs/modules/ROOT/nav.adoc b/framework-docs/modules/ROOT/nav.adoc index 276c84ee663..d1706fe3626 100644 --- a/framework-docs/modules/ROOT/nav.adoc +++ b/framework-docs/modules/ROOT/nav.adoc @@ -449,7 +449,7 @@ *** xref:languages/kotlin/null-safety.adoc[] *** xref:languages/kotlin/classes-interfaces.adoc[] *** xref:languages/kotlin/annotations.adoc[] -*** xref:languages/kotlin/bean-definition-dsl.adoc[] +*** xref:languages/kotlin/bean-registration-dsl.adoc[] *** xref:languages/kotlin/web.adoc[] *** xref:languages/kotlin/coroutines.adoc[] *** xref:languages/kotlin/spring-projects-in.adoc[] diff --git a/framework-docs/modules/ROOT/pages/core/beans/java/programmatic-bean-registration.adoc b/framework-docs/modules/ROOT/pages/core/beans/java/programmatic-bean-registration.adoc index f50c9402556..3663fc13fe7 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/java/programmatic-bean-registration.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/java/programmatic-bean-registration.adoc @@ -9,28 +9,7 @@ efficient way. Those bean registrar implementations are typically imported with an `@Import` annotation on `@Configuration` classes. -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @Import(MyBeanRegistrar.class) - class MyConfiguration { - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @Import(MyBeanRegistrar::class) - class MyConfiguration { - } ----- -====== +include-code::./MyConfiguration[tag=snippet,indent=0] NOTE: You can leverage type-level conditional annotations ({spring-framework-api}/context/annotation/Conditional.html[`@Conditional`], but also other variants) to conditionally import the related bean registrars. @@ -40,49 +19,7 @@ The bean registrar implementation uses {spring-framework-api}/beans/factory/Bean and flexible way. For example, it allows custom registration through an `if` expression, a `for` loop, etc. -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - class MyBeanRegistrar implements BeanRegistrar { - - @Override - public void register(BeanRegistry registry, Environment env) { - registry.registerBean("foo", Foo.class); - registry.registerBean("bar", Bar.class, spec -> spec - .prototype() - .lazyInit() - .description("Custom description") - .supplier(context -> new Bar(context.bean(Foo.class)))); - if (env.matchesProfiles("baz")) { - registry.registerBean(Baz.class, spec -> spec - .supplier(context -> new Baz("Hello World!"))); - } - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - class MyBeanRegistrar : BeanRegistrarDsl({ - registerBean() - registerBean( - name = "bar", - prototype = true, - lazyInit = true, - description = "Custom description") { - Bar(bean()) - } - profile("baz") { - registerBean { Baz("Hello World!") } - } - }) ----- -====== +include-code::./MyBeanRegistrar[tag=snippet,indent=0] NOTE: Bean registrars are supported with xref:core/aot.adoc[Ahead of Time Optimizations], either on the JVM or with GraalVM native images, including when instance suppliers are used. diff --git a/framework-docs/modules/ROOT/pages/languages/kotlin/bean-definition-dsl.adoc b/framework-docs/modules/ROOT/pages/languages/kotlin/bean-registration-dsl.adoc similarity index 62% rename from framework-docs/modules/ROOT/pages/languages/kotlin/bean-definition-dsl.adoc rename to framework-docs/modules/ROOT/pages/languages/kotlin/bean-registration-dsl.adoc index db7e1809ae9..da759af2bf0 100644 --- a/framework-docs/modules/ROOT/pages/languages/kotlin/bean-definition-dsl.adoc +++ b/framework-docs/modules/ROOT/pages/languages/kotlin/bean-registration-dsl.adoc @@ -1,4 +1,4 @@ -[[kotlin-bean-definition-dsl]] -= Bean Definition DSL +[[kotlin-bean-registration-dsl]] += Bean Registration DSL See xref:core/beans/java/programmatic-bean-registration.adoc[Programmatic Bean Registration]. diff --git a/framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.java b/framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.java new file mode 100644 index 00000000000..22fb1c08c4b --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.java @@ -0,0 +1,53 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration; + +import org.springframework.beans.factory.BeanRegistrar; +import org.springframework.beans.factory.BeanRegistry; +import org.springframework.core.env.Environment; +import org.springframework.web.servlet.function.RouterFunction; +import org.springframework.web.servlet.function.RouterFunctions; +import org.springframework.web.servlet.function.ServerResponse; + +// tag::snippet[] +class MyBeanRegistrar implements BeanRegistrar { + + @Override + public void register(BeanRegistry registry, Environment env) { + registry.registerBean("foo", Foo.class); + registry.registerBean("bar", Bar.class, spec -> spec + .prototype() + .lazyInit() + .description("Custom description") + .supplier(context -> new Bar(context.bean(Foo.class)))); + if (env.matchesProfiles("baz")) { + registry.registerBean(Baz.class, spec -> spec + .supplier(context -> new Baz("Hello World!"))); + } + registry.registerBean(MyRepository.class); + registry.registerBean(RouterFunction.class, spec -> + spec.supplier(context -> router(context.bean(MyRepository.class)))); + } + + RouterFunction router(MyRepository myRepository) { + return RouterFunctions.route() + // ... + .build(); + } + +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.java new file mode 100644 index 00000000000..8593224eace --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.java @@ -0,0 +1,27 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +// tag::snippet[] +@Configuration +@Import(MyBeanRegistrar.class) +class MyConfiguration { +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Bar.kt b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Bar.kt new file mode 100644 index 00000000000..eeeb6a54628 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Bar.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration + +data class Bar(val foo: Foo) \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Baz.kt b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Baz.kt new file mode 100644 index 00000000000..0dab54a5c54 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Baz.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration + +data class Baz(val value: String) \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Foo.kt b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Foo.kt new file mode 100644 index 00000000000..0942b2193a5 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Foo.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration + +class Foo \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.kt b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.kt new file mode 100644 index 00000000000..3f53879b499 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration + +import org.springframework.beans.factory.BeanRegistrarDsl +import org.springframework.web.servlet.function.router + +// tag::snippet[] +class MyBeanRegistrar : BeanRegistrarDsl({ + registerBean() + registerBean( + name = "bar", + prototype = true, + lazyInit = true, + description = "Custom description") { + Bar(bean()) + } + profile("baz") { + registerBean { Baz("Hello World!") } + } + registerBean() + registerBean(::myRouter) +}) + +fun myRouter(myRepository: MyRepository) = router { + // ... +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.kt new file mode 100644 index 00000000000..81f7c29fc84 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration + +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Import + +// tag::snippet[] +@Configuration +@Import(MyBeanRegistrar::class) +class MyConfiguration { +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyRepository.kt b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyRepository.kt new file mode 100644 index 00000000000..e40ad268baf --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyRepository.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration + +interface MyRepository { +} \ No newline at end of file