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
Copy file name to clipboardExpand all lines: src/docs/asciidoc/kotlin.adoc
+91-37Lines changed: 91 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,17 +7,19 @@
7
7
8
8
== Introduction
9
9
10
-
https://kotlinlang.org[Kotlin] is a statically-typed language targeting the JVM which allows to write concise and elegant
11
-
code while providing a very good https://kotlinlang.org/docs/reference/java-interop.html[interoperability] with libraries
10
+
https://kotlinlang.org[Kotlin] is a statically-typed language targeting the JVM (and other platforms)
11
+
which allows to write concise and elegant code while providing a very good
12
+
https://kotlinlang.org/docs/reference/java-interop.html[interoperability] with libraries
12
13
written in Java.
13
14
14
-
Spring Framework 5 introduces first-class support for Kotlin and allows developers to write Spring + Kotlin
15
-
applications almost like if Spring Framework was a native Kotlin framework.
15
+
Spring Framework 5 introduces first-class support for Kotlin and allows developers to write
16
+
Spring + Kotlin applications almost like if Spring Framework was a native Kotlin framework.
16
17
17
18
== Requirements ==
18
19
19
-
Spring Framework 5 supports Kotlin 1.1+ and requires https://bintray.com/bintray/jcenter/org.jetbrains.kotlin%3Akotlin-stdlib[`kotlin-stdlib`] (or one of its
and https://bintray.com/bintray/jcenter/org.jetbrains.kotlin%3Akotlin-reflect[`kotlin-reflect`]
23
25
to be present on the classpath. They are provided by default if you bootstrap a Kotlin project on
@@ -47,6 +49,12 @@ and Spring Framework provides some extensions to take advantage of this feature.
47
49
That allows to provide a better Kotlin API `RestTemplate`, the new `WebClient` from Spring
48
50
WebFlux and for various other API.
49
51
52
+
[NOTE]
53
+
====
54
+
Other libraries like Reactor or Spring Data also provide Kotlin extensions for their API
55
+
in order to allow a better Kotlin development experience.
56
+
====
57
+
50
58
To retrieve a list of `Foo` objects in Java you have to write:
51
59
52
60
[source,java]
@@ -66,13 +74,6 @@ val users : Flux<User> = client.get().retrieve().bodyToFlux()
66
74
Like in Java, `users` in Kotlin is strongly typed, but Kotlin clever type inference allows
67
75
shorter syntax.
68
76
69
-
70
-
[NOTE]
71
-
====
72
-
Other libraries like Reactor or Spring Data also provide Kotlin extensions for their API
73
-
in order to allow a better Kotlin development experience.
74
-
====
75
-
76
77
== Null-safety
77
78
78
79
One of Kotlin's key features is https://kotlinlang.org/docs/reference/null-safety.html[null-safety]
@@ -82,15 +83,15 @@ declarations, expressing "value or no value" semantics without paying the cost o
82
83
(Kotlin allows using functional constructs with nullable values; check out this
83
84
http://www.baeldung.com/kotlin-null-safety[comprehensive guide to Kotlin null-safety].)
84
85
85
-
Although Java does not allow to express null-safety in its type-system, Spring Framework 5 introduces
86
-
https://jira.spring.io/browse/SPR-15540[null-safety of the whole Spring Framework APIs]
86
+
Although Java does not allow to express null-safety in its type-system, Spring Framework now
87
+
provides https://jira.spring.io/browse/SPR-15540[null-safety of the whole Spring Framework API]
87
88
via tooling-friendly annotations:
88
89
89
-
* `@NonNullApi` annotations at package level declare that non-null is the default behavior
90
+
* `@NonNullApi` annotations at package level declare non-null as the default behavior
90
91
* `@Nullable` annotations where specific parameters or return values can be `null`.
91
92
92
93
Both annotations are meta-annotated with https://jcp.org/en/jsr/detail?id=305[JSR 305]
93
-
meta-annotations (a dormant JSR but supported by tools like IDEA, Eclipse, Findbugs, etc.)
94
+
meta-annotations (a dormant JSR but supported by tools like IDEA, Findbugs, etc.)
94
95
to provide useful warnings to Java developers.
95
96
96
97
On the Kotlin side - as of the https://blog.jetbrains.com/kotlin/2017/08/kotlin-1-1-4-is-out/[Kotlin 1.1.4 release] -
@@ -106,9 +107,6 @@ the default behavior in an upcoming release of Kotlin.
106
107
Make sure to https://github.com/sdeleuze/spring-kotlin-functional/blob/2d6ac07adfc2b8f25e91681dbb2b58a1c6cdf9a7/build.gradle.kts#L57[include JSR-305 JAR]
107
108
until Kotlin 1.1.5 is released (it will fix https://youtrack.jetbrains.com/issue/KT-19419[KT-19419]).
108
109
109
-
Currently null-safety does not apply to generic type parameters, but that could change in
110
-
the future, the related issue is https://youtrack.jetbrains.com/issue/KT-19592[KT-19592].
111
-
112
110
[NOTE]
113
111
====
114
112
Other libraries like Reactor or Spring Data leverage these annotations to provide
@@ -117,7 +115,7 @@ null-safe APIs for Kotlin developers.
117
115
118
116
== Classes & Interfaces
119
117
120
-
Spring Framework 5 now supports various Kotlin constructs like instantiating Kotlin classes
118
+
Spring Framework supports various Kotlin constructs like instantiating Kotlin classes
121
119
via primary constructors, immutable classes data binding and function optional parameters
122
120
with default values.
123
121
@@ -127,8 +125,8 @@ compiler flag.
127
125
128
126
https://github.com/FasterXML/jackson-module-kotlin[Jackson Kotlin module] which is required
129
127
for serializing / deserializing JSON data is automatically registered when present in the
130
-
classpath, and will log a warning message if Jackson + Kotlin are detected without Jackson
131
-
Kotlin module.
128
+
classpath, and a warning message will be logged if Jackson + Kotlin are detected without
129
+
Jackson Kotlin module.
132
130
133
131
[NOTE]
134
132
====
@@ -140,7 +138,6 @@ As of Spring Boot 2.0, Jackson Kotlin module is automatically provided via the J
140
138
Spring Framework also takes advantage of https://kotlinlang.org/docs/reference/null-safety.html[Kotlin null-safety]
141
139
to determine if an HTTP parameter is required without having to define explicitly the `required` attribute.
142
140
That means `@RequestParam name: String?` with be treated as not required and `@RequestParam name: String` as required.
143
-
144
141
This is also supported on Spring Messaging `@Header` annotation.
145
142
146
143
In a similar fashion, Spring bean injection with `@Autowired` or `@Inject` uses this information
@@ -150,8 +147,8 @@ won’t raise an error if such bean does not exist.
150
147
151
148
== Bean definition DSL
152
149
153
-
Spring Framework 5 introduces a new way to register beans in a functional way using lambda
154
-
as an alternative to XML or JavaConfig with `@Configuration` and `@Bean`. In a nutshell,
150
+
Spring Framework 5 introduces a new way to register beans in a functional way using lambdas
151
+
as an alternative to XML or JavaConfig (`@Configuration` and `@Bean`). In a nutshell,
155
152
it makes it possible to register beans with a lambda that acts as a `FactoryBean`.
156
153
It is very efficient and does not require any reflection or CGLIB proxies.
157
154
@@ -176,8 +173,8 @@ val context = GenericApplicationContext().apply {
176
173
}
177
174
----
178
175
179
-
In order to allow a more declarative approach and cleaner syntax, Spring Framework 5 introduces
180
-
a new {doc-root}/spring-framework/docs/{spring-version}/kdoc-api/spring-framework/org.springframework.context.support/-bean-definition-dsl/[Kotlin bean definition DSL]
176
+
In order to allow a more declarative approach and cleaner syntax, Spring Framework provides
177
+
a {doc-root}/spring-framework/docs/{spring-version}/kdoc-api/spring-framework/org.springframework.context.support/-bean-definition-dsl/[Kotlin bean definition DSL]
181
178
It declares an `ApplicationContextInitializer` via a clean declarative API which allows
182
179
you to deal with profiles and `Environment` for customizing how your beans are registered.
183
180
@@ -240,16 +237,18 @@ for a concrete example.
240
237
241
238
[NOTE]
242
239
====
243
-
Spring Boot is based on Java Config, but should allow using user-defined functional bean definitions,
244
-
see https://jira.spring.io/browse/SPR-13779[SPR-13779] and https://github.com/spring-projects/spring-boot/issues/8115[spring-boot/#8115]
240
+
Spring Boot is based on Java Config and
241
+
https://github.com/spring-projects/spring-boot/issues/8115[does not provide specific support for functional bean definition yet],
242
+
but you can experimentally use functional bean definitions via its `ApplicationContextInitializer` support,
243
+
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]
0 commit comments