joaomiguelpereira / spring-fu

Explicit configuration for Spring Boot using Kotlin DSLs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status API documentation

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:

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 of WebApplicationType.SERVLET

  • Use webFlux { } instead of webMvc { }

  • Use spring-boot-starter-webflux starter instead of spring-boot-starter-web

  • Use coRouter { } instead of router { } 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:

About

Explicit configuration for Spring Boot using Kotlin DSLs

License:Apache License 2.0


Languages

Language:Kotlin 62.8%Language:Java 37.0%Language:Shell 0.1%Language:HTML 0.0%