Skip to content

Commit 8d951c5

Browse files
committed
Document BeanRegistrarDsl with RouterFunctionDsl
Closes gh-35549
1 parent 65daea1 commit 8d951c5

File tree

11 files changed

+231
-68
lines changed

11 files changed

+231
-68
lines changed

framework-docs/modules/ROOT/nav.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@
449449
*** xref:languages/kotlin/null-safety.adoc[]
450450
*** xref:languages/kotlin/classes-interfaces.adoc[]
451451
*** xref:languages/kotlin/annotations.adoc[]
452-
*** xref:languages/kotlin/bean-definition-dsl.adoc[]
452+
*** xref:languages/kotlin/bean-registration-dsl.adoc[]
453453
*** xref:languages/kotlin/web.adoc[]
454454
*** xref:languages/kotlin/coroutines.adoc[]
455455
*** xref:languages/kotlin/spring-projects-in.adoc[]

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

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,7 @@ efficient way.
99
Those bean registrar implementations are typically imported with an `@Import` annotation
1010
on `@Configuration` classes.
1111

12-
[tabs]
13-
======
14-
Java::
15-
+
16-
[source,java,indent=0,subs="verbatim,quotes"]
17-
----
18-
@Configuration
19-
@Import(MyBeanRegistrar.class)
20-
class MyConfiguration {
21-
}
22-
----
23-
24-
Kotlin::
25-
+
26-
[source,kotlin,indent=0,subs="verbatim,quotes"]
27-
----
28-
@Configuration
29-
@Import(MyBeanRegistrar::class)
30-
class MyConfiguration {
31-
}
32-
----
33-
======
12+
include-code::./MyConfiguration[tag=snippet,indent=0]
3413

3514
NOTE: You can leverage type-level conditional annotations ({spring-framework-api}/context/annotation/Conditional.html[`@Conditional`],
3615
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
4019
and flexible way. For example, it allows custom registration through an `if` expression, a
4120
`for` loop, etc.
4221

43-
[tabs]
44-
======
45-
Java::
46-
+
47-
[source,java,indent=0,subs="verbatim,quotes"]
48-
----
49-
class MyBeanRegistrar implements BeanRegistrar {
50-
51-
@Override
52-
public void register(BeanRegistry registry, Environment env) {
53-
registry.registerBean("foo", Foo.class);
54-
registry.registerBean("bar", Bar.class, spec -> spec
55-
.prototype()
56-
.lazyInit()
57-
.description("Custom description")
58-
.supplier(context -> new Bar(context.bean(Foo.class))));
59-
if (env.matchesProfiles("baz")) {
60-
registry.registerBean(Baz.class, spec -> spec
61-
.supplier(context -> new Baz("Hello World!")));
62-
}
63-
}
64-
}
65-
----
66-
67-
Kotlin::
68-
+
69-
[source,kotlin,indent=0,subs="verbatim,quotes"]
70-
----
71-
class MyBeanRegistrar : BeanRegistrarDsl({
72-
registerBean<Foo>()
73-
registerBean(
74-
name = "bar",
75-
prototype = true,
76-
lazyInit = true,
77-
description = "Custom description") {
78-
Bar(bean<Foo>())
79-
}
80-
profile("baz") {
81-
registerBean { Baz("Hello World!") }
82-
}
83-
})
84-
----
85-
======
22+
include-code::./MyBeanRegistrar[tag=snippet,indent=0]
8623

8724
NOTE: Bean registrars are supported with xref:core/aot.adoc[Ahead of Time Optimizations],
8825
either on the JVM or with GraalVM native images, including when instance suppliers are used.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[[kotlin-bean-definition-dsl]]
2-
= Bean Definition DSL
1+
[[kotlin-bean-registration-dsl]]
2+
= Bean Registration DSL
33

44
See xref:core/beans/java/programmatic-bean-registration.adoc[Programmatic Bean Registration].
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration;
18+
19+
import org.springframework.beans.factory.BeanRegistrar;
20+
import org.springframework.beans.factory.BeanRegistry;
21+
import org.springframework.core.env.Environment;
22+
import org.springframework.web.servlet.function.RouterFunction;
23+
import org.springframework.web.servlet.function.RouterFunctions;
24+
import org.springframework.web.servlet.function.ServerResponse;
25+
26+
// tag::snippet[]
27+
class MyBeanRegistrar implements BeanRegistrar {
28+
29+
@Override
30+
public void register(BeanRegistry registry, Environment env) {
31+
registry.registerBean("foo", Foo.class);
32+
registry.registerBean("bar", Bar.class, spec -> spec
33+
.prototype()
34+
.lazyInit()
35+
.description("Custom description")
36+
.supplier(context -> new Bar(context.bean(Foo.class))));
37+
if (env.matchesProfiles("baz")) {
38+
registry.registerBean(Baz.class, spec -> spec
39+
.supplier(context -> new Baz("Hello World!")));
40+
}
41+
registry.registerBean(MyRepository.class);
42+
registry.registerBean(RouterFunction.class, spec ->
43+
spec.supplier(context -> router(context.bean(MyRepository.class))));
44+
}
45+
46+
RouterFunction<ServerResponse> router(MyRepository myRepository) {
47+
return RouterFunctions.route()
48+
// ...
49+
.build();
50+
}
51+
52+
}
53+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration;
18+
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.context.annotation.Import;
21+
22+
// tag::snippet[]
23+
@Configuration
24+
@Import(MyBeanRegistrar.class)
25+
class MyConfiguration {
26+
}
27+
// end::snippet[]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration
18+
19+
data class Bar(val foo: Foo)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration
18+
19+
data class Baz(val value: String)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration
18+
19+
class Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration
18+
19+
import org.springframework.beans.factory.BeanRegistrarDsl
20+
import org.springframework.web.servlet.function.router
21+
22+
// tag::snippet[]
23+
class MyBeanRegistrar : BeanRegistrarDsl({
24+
registerBean<Foo>()
25+
registerBean(
26+
name = "bar",
27+
prototype = true,
28+
lazyInit = true,
29+
description = "Custom description") {
30+
Bar(bean<Foo>())
31+
}
32+
profile("baz") {
33+
registerBean { Baz("Hello World!") }
34+
}
35+
registerBean<MyRepository>()
36+
registerBean(::myRouter)
37+
})
38+
39+
fun myRouter(myRepository: MyRepository) = router {
40+
// ...
41+
}
42+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.java.beansjavaprogrammaticregistration
18+
19+
import org.springframework.context.annotation.Configuration
20+
import org.springframework.context.annotation.Import
21+
22+
// tag::snippet[]
23+
@Configuration
24+
@Import(MyBeanRegistrar::class)
25+
class MyConfiguration {
26+
}
27+
// end::snippet[]

0 commit comments

Comments
 (0)