Browse Source

Document BeanRegistrarDsl with RouterFunctionDsl

Closes gh-35549
pull/35696/head
Sébastien Deleuze 2 months ago
parent
commit
8d951c569b
  1. 2
      framework-docs/modules/ROOT/nav.adoc
  2. 67
      framework-docs/modules/ROOT/pages/core/beans/java/programmatic-bean-registration.adoc
  3. 4
      framework-docs/modules/ROOT/pages/languages/kotlin/bean-registration-dsl.adoc
  4. 53
      framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.java
  5. 27
      framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.java
  6. 19
      framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Bar.kt
  7. 19
      framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Baz.kt
  8. 19
      framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Foo.kt
  9. 42
      framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.kt
  10. 27
      framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.kt
  11. 20
      framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyRepository.kt

2
framework-docs/modules/ROOT/nav.adoc

@ -449,7 +449,7 @@ @@ -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[]

67
framework-docs/modules/ROOT/pages/core/beans/java/programmatic-bean-registration.adoc

@ -9,28 +9,7 @@ efficient way. @@ -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 @@ -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<Foo>()
registerBean(
name = "bar",
prototype = true,
lazyInit = true,
description = "Custom description") {
Bar(bean<Foo>())
}
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.

4
framework-docs/modules/ROOT/pages/languages/kotlin/bean-definition-dsl.adoc → framework-docs/modules/ROOT/pages/languages/kotlin/bean-registration-dsl.adoc

@ -1,4 +1,4 @@ @@ -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].

53
framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.java

@ -0,0 +1,53 @@ @@ -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<ServerResponse> router(MyRepository myRepository) {
return RouterFunctions.route()
// ...
.build();
}
}
// end::snippet[]

27
framework-docs/src/main/java/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.java

@ -0,0 +1,27 @@ @@ -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[]

19
framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Bar.kt

@ -0,0 +1,19 @@ @@ -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)

19
framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Baz.kt

@ -0,0 +1,19 @@ @@ -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)

19
framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/Foo.kt

@ -0,0 +1,19 @@ @@ -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

42
framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyBeanRegistrar.kt

@ -0,0 +1,42 @@ @@ -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<Foo>()
registerBean(
name = "bar",
prototype = true,
lazyInit = true,
description = "Custom description") {
Bar(bean<Foo>())
}
profile("baz") {
registerBean { Baz("Hello World!") }
}
registerBean<MyRepository>()
registerBean(::myRouter)
})
fun myRouter(myRepository: MyRepository) = router {
// ...
}
// end::snippet[]

27
framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyConfiguration.kt

@ -0,0 +1,27 @@ @@ -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[]

20
framework-docs/src/main/kotlin/org/springframework/docs/core/beans/java/beansjavaprogrammaticregistration/MyRepository.kt

@ -0,0 +1,20 @@ @@ -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 {
}
Loading…
Cancel
Save