对于最新稳定版本,请使用 Spring Boot 4.0.4spring-doc.cadn.net.cn

Kotlin 支持

Kotlin 是一门面向 JVM(以及其他平台)的静态类型语言,它允许编写简洁而优雅的代码,同时提供与用 Java 编写的现有库的互操作性spring-doc.cadn.net.cn

Spring Boot 通过利用其他 Spring 项目(如 Spring Framework、Spring Data 和 Reactor)的支持来提供 Kotlin 支持。 See the Spring Framework Kotlin support documentation for more information.spring-doc.cadn.net.cn

The easiest way to start with Spring Boot and Kotlin is to follow 这个全面的教程。 You can create new Kotlin projects by using start.spring.io。 Feel free to join the #spring channel of Kotlin Slack or ask a question with the spring and kotlin tags on Stack Overflow if you need support.spring-doc.cadn.net.cn

要求

Spring Boot 要求至少使用 Kotlin 1.7.x,并通过依赖管理来管理合适的 Kotlin 版本。 要使用 Kotlin,必须在类路径上包含 org.jetbrains.kotlin:kotlin-stdliborg.jetbrains.kotlin:kotlin-reflectkotlin-stdlib 变体 kotlin-stdlib-jdk7kotlin-stdlib-jdk8 也可以使用。spring-doc.cadn.net.cn

由于Kotlin 类默认是 final 的,你很可能需要配置kotlin-spring插件,以便自动将带有 Spring 注解的类打开(open),使其能够被代理。spring-doc.cadn.net.cn

Jackson 的 Kotlin 模块 是在 Kotlin 中序列化/反序列化 JSON 数据所必需的。 当它在类路径中被发现时,会自动注册。spring-doc.cadn.net.cn

如果在 start.spring.io 上引导一个 Kotlin 项目,这些依赖项和插件将默认提供。

Null-safety

Kotlin 的关键特性之一是空安全。 它在编译时处理null值,而不是将问题推迟到运行时并遇到NullPointerException。 这有助于消除一个常见的错误来源,而无需付出像Optional这样的包装器的代价。 Kotlin 还允许对可空值使用函数式构造,如本Kotlin 空安全综合指南中所述。spring-doc.cadn.net.cn

尽管 Java 的类型系统不允许表达空安全(null-safety),但 Spring Framework、Spring Data 和 Reactor 现在通过友好的注解提供了它们 API 的空安全。 默认情况下,Kotlin 中使用的 Java API 类型被视为 平台类型,其中的空检查是宽松的。 Kotlin 对 JSR 305 注解的支持 结合 nullability 注解为相关 Spring API 在 Kotlin 中提供了空安全。spring-doc.cadn.net.cn

JSR 305 检查可以通过添加以下编译标志来配置:-Xjsr305。 默认行为与 -Xjsr305={strict|warn|ignore} 相同。 -Xjsr305=warn 值要求在 Kotlin 类型从 Spring API 推断时考虑 null 安全性,但应知道 Spring API 的 null 性声明可能会在不同次要版本之间发生变化,并且将来可能还会增加更多的检查。spring-doc.cadn.net.cn

泛型类型参数、可变参数(varargs)和数组元素的可空性(nullability)目前尚不支持。 有关最新信息,请参见 SPR-15942。 另外请注意,Spring Boot 自身的 API 目前尚未添加注解

Kotlin API

运行应用程序

Spring Boot 提供了一种 idiomatic 的方式来运行一个应用,如以下示例所示: runApplication<MyApplication>(*args)spring-doc.cadn.net.cn

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class MyApplication

fun main(args: Array<String>) {
	runApplication<MyApplication>(*args)
}

这是SpringApplication.run(MyApplication::class.java, *args)的一个即插即用替代方案。 它还允许如以下示例所示自定义应用程序:spring-doc.cadn.net.cn

runApplication<MyApplication>(*args) {
	setBannerMode(OFF)
}

扩展

Kotlin extensions 提供了扩展现有类以添加额外功能的能力。 The Spring Boot Kotlin API 利用这些扩展来为现有的 API 添加新的 Kotlin 特定便利功能。spring-doc.cadn.net.cn

TestRestTemplate 扩展,类似于 Spring Framework 为 RestOperations 在 Spring Framework 中提供的扩展,现已提供。 除其他功能外,这些扩展使得利用 Kotlin 的实化类型参数成为可能。spring-doc.cadn.net.cn

依赖管理

为了避免类路径上混入不同版本的Kotlin依赖项,Spring Boot导入了Kotlin BOM。spring-doc.cadn.net.cn

使用 Maven,可以通过设置 kotlin.version 属性来自定义 Kotlin 版本,并提供 kotlin-maven-plugin 插件管理。 使用 Gradle 时,Spring Boot 插件会自动将 kotlin.version 对应于 Kotlin 插件的版本。spring-doc.cadn.net.cn

Spring Boot 也通过导入Kotlin Coroutines BOM 来管理Coroutines 的版本。 该版本可以通过设置kotlin-coroutines.version属性来自定义。spring-doc.cadn.net.cn

org.jetbrains.kotlinx:kotlinx-coroutines-reactor 依赖项会在使用 start.spring.io 初始化包含至少一个响应式依赖的 Kotlin 项目时默认提供。

@ConfigurationProperties

@ConfigurationProperties构造函数绑定 结合使用时,支持具有不可变 val 属性的数据类,如下例所示:spring-doc.cadn.net.cn

@ConfigurationProperties("example.kotlin")
data class KotlinExampleProperties(
		val name: String,
		val description: String,
		val myService: MyService) {

	data class MyService(
			val apiToken: String,
			val uri: URI
	)
}

由于值类与Java的互操作性有限,因此对其支持是有限的。 特别是,依赖于值类的默认值将无法与配置属性绑定工作。 在这种情况下,应使用数据类代替。spring-doc.cadn.net.cn

要使用注解处理器生成您自己的元数据,应将 kapt 配置 为包含 spring-boot-configuration-processor 依赖项。 请注意,由于 kapt 提供的模型存在限制,某些功能(例如检测默认值或已弃用的项)无法正常工作。

测试

虽然可以使用 JUnit 4 来测试 Kotlin 代码,但默认提供并推荐使用 JUnit 5。 JUnit 5 允许测试类只实例化一次,并在该类的所有测试中重复使用。 这使得可以在非静态方法上使用 @BeforeAll@AfterAll 注解,这非常适合 Kotlin。spring-doc.cadn.net.cn

要模拟 Kotlin 类,推荐使用 MockK。 如果您需要相当于 Mockito 特有的 @MockitoBean@MockitoSpyBean 注解的 MockK 等价物,可以使用 SpringMockK,它提供了类似的 @MockkBean@SpykBean 注解。spring-doc.cadn.net.cn