Spring Fu is an incubator for Kofu, a set of
Kotlin DSLs like application { }
and configuration { }
designed to configure Spring Boot explicitly with code in a declarative way with great
discoverability thanks to auto-complete. In Kofu, "Ko" stands for Kotlin, and "fu" for functional.
It leverages other Spring Kotlin DSLs available in Spring like:
-
beans { }
DSL from Spring Framework -
router { }
DSL from Spring MVC or Spring WebFlux -
security { }
DSL (work in progress) from Spring Security
It provides fast startup (40% faster than regular auto-configuration on a minimal Spring MVC app) and low memory consumption.
It is not intended to be used in production yet, but rather to incubate and get feedback and contributions from the community in order to hopefully reach a point where it can be integrated as Spring Boot Kotlin DSL.
If you are interested in running Spring application as GraalVM native images see the spring-graal-native-image project.
Sample application
Here is a sample sample application that is leveraging Spring MVC:
val app = application(WebApplicationType.SERVLET) {
logging {
level = LogLevel.DEBUG
}
beans {
bean<SampleService>()
}
webMvc {
port = if (profiles.contains("test")) 8181 else 8080
router {
val service = ref<SampleService>()
GET("/") {
ok().body(service.generateMessage())
}
GET("/api") {
ok().body(Sample(service.generateMessage()))
}
}
converters {
string()
jackson {
indentOutput = true
}
}
}
}
data class Sample(val message: String)
class SampleService {
fun generateMessage() = "Hello world!"
}
fun main() {
app.run()
}
To use WebFlux.fn instead
-
Use
WebApplicationType.REACTIVE
instead ofWebApplicationType.SERVLET
-
Use
webFlux { }
instead ofwebMvc { }
-
Use
spring-boot-starter-webflux
starter instead ofspring-boot-starter-web
-
Use
coRouter { }
instead ofrouter { }
if you want to use Coroutines instead of Reactor API
Dependencies
Kofu is technically just a dependency you add to your Spring Boot project.
repositories {
mavenCentral()
maven("https://repo.spring.io/milestone")
}
dependencies {
implementation("org.springframework.fu:spring-fu-kofu:0.2.2")
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-webflux")
}
Getting started
-
Go to start.spring.io
-
Select latest Spring Boot
2.2.0.x
release -
Select the "Web" or "Reactive web" starter
-
Add the
org.springframework.fu:spring-fu-kofu:0.2.2
dependency -
Modify the generated
DemoApplication.kt
file as following:
package com.sample
import org.springframework.fu.kofu.application
val app = application(...) {
...
}
fun main() {
app.run()
}
See sample projects here.
You can now also benefits of Spring Framework reference documentation which is now available with Kotlin code samples.
Credits
In addition to the whole Spring and Reactor teams, special credits to:
-
Juergen Hoeller for his support on Kotlin and the functional bean registration API
-
Arjen Poutsma for creating the WebFlux functional API
-
Thomas Girard for its spring-webflux-kotlin-dsl experiment that initially demonstrated this approach was possible
-
Konrad Kaminski for his awesome spring-kotlin-coroutine project
-
Dave Syer for his work on benchmarks, GraalVM support and functional bean registration applied to Boot
-
The whole Spring Boot team