You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -263,16 +282,20 @@ as the following example shows:
263
282
}
264
283
----
265
284
266
-
267
285
NOTE: Spring Boot is based on JavaConfig and
268
286
https://github.com/spring-projects/spring-boot/issues/8115[does not yet provide specific support for functional bean definition],
269
287
but you can experimentally use functional bean definitions through Spring Boot's `ApplicationContextInitializer` support.
270
288
See https://stackoverflow.com/questions/45935931/how-to-use-functional-bean-definition-kotlin-dsl-with-spring-boot-and-spring-w/46033685#46033685[this Stack Overflow answer]
271
289
for more details and up-to-date information. See also the experimental Kofu DSL developed in https://github.com/spring-projects/spring-fu[Spring Fu incubator].
272
290
291
+
292
+
293
+
273
294
[[kotlin-web]]
274
295
== Web
275
296
297
+
298
+
276
299
=== Router DSL
277
300
278
301
Spring Framework comes with a Kotlin router DSL available in 3 flavors:
@@ -314,6 +337,8 @@ when you need to register routes depending on dynamic data (for example, from a
314
337
315
338
See https://github.com/mixitconf/mixit/[MiXiT project] for a concrete example.
316
339
340
+
341
+
317
342
=== MockMvc DSL
318
343
319
344
A Kotlin DSL is provided via `MockMvc` Kotlin extensions in order to provide a more
Read this blog post about https://spring.io/blog/2019/04/12/going-reactive-with-spring-coroutines-and-kotlin-flow[Going Reactive with Spring, Coroutines and Kotlin Flow]
459
493
for more details, including how to run code concurrently with Coroutines.
460
494
495
+
496
+
461
497
=== Controllers
462
498
463
499
Here is an example of a Coroutines `@RestController`.
@@ -554,6 +590,8 @@ class CoroutinesViewController(banner: Banner) {
554
590
}
555
591
----
556
592
593
+
594
+
557
595
=== WebFlux.fn
558
596
559
597
Here is an example of Coroutines router defined via the {docs-spring-framework}/kdoc-api/spring-webflux/org.springframework.web.reactive.function.server/co-router.html[coRouter { }] DSL and related handlers.
@@ -587,6 +625,8 @@ class UserHandler(builder: WebClient.Builder) {
587
625
}
588
626
----
589
627
628
+
629
+
590
630
=== Transactions
591
631
592
632
Transactions on Coroutines are supported via the programmatic variant of the Reactive
@@ -636,6 +676,8 @@ For Kotlin `Flow`, a `Flow<T>.transactional` extension is provided.
636
676
----
637
677
638
678
679
+
680
+
639
681
[[kotlin-spring-projects-in-kotlin]]
640
682
== Spring Projects in Kotlin
641
683
@@ -683,6 +725,8 @@ NOTE: The Kotlin code samples in Spring Framework documentation do not explicitl
683
725
`open` on the classes and their member functions. The samples are written for projects
684
726
using the `kotlin-allopen` plugin, since this is the most commonly used setup.
685
727
728
+
729
+
686
730
=== Using Immutable Class Instances for Persistence
687
731
688
732
In Kotlin, it is convenient and considered to be a best practice to declare read-only properties
@@ -726,6 +770,8 @@ NOTE: As of the Kay release train, Spring Data supports Kotlin immutable class i
726
770
does not require the `kotlin-noarg` plugin if the module uses Spring Data object mappings
727
771
(such as MongoDB, Redis, Cassandra, and others).
728
772
773
+
774
+
729
775
=== Injecting Dependencies
730
776
731
777
Our recommendation is to try to favor constructor injection with `val` read-only (and
@@ -761,6 +807,8 @@ as the following example shows:
761
807
}
762
808
----
763
809
810
+
811
+
764
812
=== Injecting Configuration Properties
765
813
766
814
In Java, you can inject configuration properties by using annotations (such as pass:q[`@Value("${property}")`)].
@@ -801,6 +849,7 @@ that uses the `${...}` syntax, with configuration beans, as the following exampl
801
849
----
802
850
803
851
852
+
804
853
=== Checked Exceptions
805
854
806
855
Java and https://kotlinlang.org/docs/reference/exceptions.html[Kotlin exception handling]
@@ -813,6 +862,8 @@ To get the original exception thrown like in Java, methods should be annotated w
The easiest way to start a new Spring Framework project in Kotlin is to create a new Spring
980
1042
Boot 2 project on https://start.spring.io/#!language=kotlin&type=gradle-project[start.spring.io].
981
1043
1044
+
1045
+
982
1046
=== Choosing the Web Flavor
983
1047
984
1048
Spring Framework now comes with two different web stacks: <<web#mvc, Spring MVC>> and
@@ -991,6 +1055,9 @@ Kotlin DSL.
991
1055
For other use cases, especially if you are using blocking technologies such as JPA, Spring
992
1056
MVC and its annotation-based programming model is the recommended choice.
993
1057
1058
+
1059
+
1060
+
994
1061
[[kotlin-resources]]
995
1062
== Resources
996
1063
@@ -1004,6 +1071,8 @@ Kotlin and the Spring Framework:
1004
1071
* https://blog.jetbrains.com/kotlin/[Kotlin blog]
1005
1072
* https://kotlin.link/[Awesome Kotlin]
1006
1073
1074
+
1075
+
1007
1076
=== Examples
1008
1077
1009
1078
The following Github projects offer examples that you can learn from and possibly even extend:
@@ -1016,6 +1085,8 @@ The following Github projects offer examples that you can learn from and possibl
1016
1085
* https://github.com/sdeleuze/spring-kotlin-deepdive[spring-kotlin-deepdive]: A step-by-step migration guide for Boot 1.0 and Java to Boot 2.0 and Kotlin
1017
1086
* https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-kotlin-samples/spring-cloud-gcp-kotlin-app-sample[spring-cloud-gcp-kotlin-app-sample]: Spring Boot with Google Cloud Platform Integrations
1018
1087
1088
+
1089
+
1019
1090
=== Issues
1020
1091
1021
1092
The following list categorizes the pending issues related to Spring and Kotlin support:
0 commit comments