Skip to content

Explicit configuration for Spring Boot using Kotlin and Kofu DSL

License

Notifications You must be signed in to change notification settings

EasySpringBoot/spring-fu

 
 

Repository files navigation

Build Status API documentation

Spring Fu is an incubator for Kofu (Ko for Kotlin, fu for functional), which provides a Kotlin API to configure Spring Boot applications programmatically with following characteristics:

  • Explicit configuration via a Kotlin DSL instead of auto-configuration

  • Leverages Spring Framework 5 functional bean configuration instead of annotations

  • Allows to define custom configuration slices (useful for more focused and efficient testing)

  • Great discoverability via code auto-complete

  • Web functional routing instead of @Controller available in 3 flavors:

  • Persistence via Spring Data functional APIs like:

  • Configuration via Spring Security security { } DSL (WIP)

  • Fast startup 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 part of Spring Boot.

If you are interested in running Spring application as GraalVM native images see this dedicated Spring Framework issue.

Minimal application

Here is a minimal sample application that is leveraging WebMvc.fn:

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.

dependencies {
	implementation("org.springframework.fu:spring-fu-kofu:0.x")

	implementation("org.springframework.boot:spring-boot-starter-web")
	testImplementation("org.springframework.boot:spring-boot-starter-webflux")
}

Getting started

  • Create a Spring 2.2.0.M3 project on start.spring.io with the "Web" or "Reactive web" starter

  • Add the org.springframework.fu:spring-fu-kofu:0.1 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.

Credits

In addition to the whole Spring and Reactor teams, special credits to:

About

Explicit configuration for Spring Boot using Kotlin and Kofu DSL

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 62.9%
  • Java 37.0%
  • Other 0.1%