Spring Boot

Spring Boot is an open source Java-based framework made to ease the bootstrapping and development of the Spring applications. It is developed by Pivotal, and is a part of the Spring Framework. It gives you easy access to rich Spring ecosystem, with many enterprise-grade libraries and technologies. Spring Boot applications can be written in Kotlin and the language is officially supported by the framework.

Build configuration

The integration with Spring Boot is contained in the kvision-server-spring-boot module. It has to be added as the dependency in the common target. This module depends on the spring-boot-starter, spring-boot-starter-webflux, spring-boot-starter-security and spring-data-relational. Any other dependencies can be added to build.gradle.kts and then be used in your application.

build.gradle.kts
dependencies {
    implementation(kotlin("stdlib-jdk8"))
    implementation(kotlin("reflect"))
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.springframework.boot:spring-boot-devtools")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot.experimental:spring-boot-actuator-autoconfigure-r2dbc:$springAutoconfigureR2dbcVersion")
    implementation("org.springframework.data:spring-data-r2dbc:$springDataR2dbcVersion")
    implementation("io.r2dbc:r2dbc-postgresql:$r2dbcPostgresqlVersion")
    implementation("io.r2dbc:r2dbc-h2:$r2dbcH2Version")
}

Application configuration

The standard way to configure Spring Boot application is src/jvmMain/resources/application.yml file. It contains options needed for optional dependencies. It can be empty if they are not used.

Implementation

Service class

The implementation of the service class comes down to implementing required interface methods and making it a Spring component by using a @Service annotation with a "prototype" scope.

Spring IoC (Inversion of Control) allows you to inject resources and other Spring components into your service class. You can use standard Spring @Autowired annotation or constructor based injection.

You can also inject custom Spring components, defined throughout your application.

KVision allows you to inject ServerRequest object, which gives you access to the user session as well.

Note: The new instance of the service class will be created by Spring for every server request. Use session or request objects to store your state with appropriate scope.

Blocking code

Since Spring WebFlux architecture is asynchronous and non-blocking, you should not block threads in your application code. If you have to use some blocking code (e.g. blocking I/O, JDBC) use the dedicated coroutine dispatcher.

Application class

To allow KVision work with Spring Boot you have to pass all instances of the KVServiceManager objects (defined in common code) to the Spring environment. You do this by defining a provider method for the List<KVServiceManager<Any>> instance in the main application class.

Use @EnableAutoConfiguration annotation to disable security if your app doesn't need it.

Authentication

You can use standard Spring WebFlux Security configuration, and with a help of serviceMatchers extension function, you can automatically select endpoints that should be secured.