Kotlin Help

Kotlin-spring compiler plugin

The kotlin-spring compiler plugin configures Kotlin projects for Spring by automatically making certain Spring-annotated classes and members open at compile time.

This plugin is a preconfigured wrapper around the all-open plugin and behaves the same way: it makes a class and its members open when they have a Spring annotation. You can use both all-open and kotlin-spring in the same project.

Supported Spring annotations

The plugin makes a class and its members open when the class has any of the following Spring annotations:

The plugin also supports meta-annotations. Classes annotated with @Configuration, @Controller, @RestController, @Service, or @Repository also become open, because Spring meta-annotates these annotations with @Component.

Apply the plugin

Gradle

Add the kotlin-spring plugin in the plugins {} block of your build.gradle(.kts) file:

plugins { kotlin("jvm") version "2.3.21" // The version of Kotlin to use kotlin("plugin.spring") version "2.3.21" // The Kotlin Spring plugin }
plugins { id "org.jetbrains.kotlin.jvm" version "2.3.21" id "org.jetbrains.kotlin.plugin.spring" version "2.3.21" }

Maven

In Maven, support for the kotlin-spring plugin is provided by the kotlin-maven-allopen plugin. Add it to your pom.xml file:

<properties> <kotlin.version>2.3.21</kotlin.version> </properties> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <configuration> <args> <arg>-Xjsr305=strict</arg> </args> <compilerPlugins> <plugin>spring</plugin> </compilerPlugins> </configuration> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-allopen</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin>

Use the kotlin-spring plugin

When you apply the plugin, the compiler treats classes with common Spring annotations as open at compile time. You can write these classes without adding the open keyword:

import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/") class MessageController { // This class and all its members are open @GetMapping fun listMessages() = listOf( Message("1", "Hello!"), Message("2", "Bonjour!"), Message("3", "Privet!"), ) }

What's next?

  • For JPA entities that require a zero-argument constructor, use the kotlin-jpa plugin, which configures the no-arg plugin with JPA annotations such as @Entity, @Embeddable, and @MappedSuperclass.

  • Create your first Spring Boot application using the Get started with Spring Boot and Kotlin tutorial.

21 October 2026