> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/47.-kotlin-support/47.2-null-safety.md).

# 47.2 空安全

[空安全](https://kotlinlang.org/docs/reference/null-safety.html)是Kotlin的关键特性之一。它会在编译时处理`null`值，而不是等到运行时才处理然后不幸遇到了`NullPointerException`。这有助于消除一个普遍的bug源头，并且不需要花费包装器的开销，比如`Optional`。Kotlin也允许使用带有可空值的函数式的构造器。就像[Kotlin里空安全的全面指南](http://www.baeldung.com/kotlin-null-safety)里描述的那样。

尽管Java不允许在它的类型系统里表达空安全，Spring框架、Spring Data和Reactor现在通过工具友好的注解，提供了它们API的空安全。默认的，Kotlin里使用的来自Java API的类型被认为是[平台类型](https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types)。平台类型的null检查是宽松的。[对JSR 305注解的Kotlin支持](https://kotlinlang.org/docs/reference/java-interop.html#jsr-305-support)连同为空性注解，提供了Kotlin里相关Spring API的空安全。

JSR 305检查可以通过添加`-Xjsr305`编译器标记配置。该标识可以是以下选项：`-Xjsr305={strict|warn|ignore}`。默认行为与`-Xjsr305=warn`相同。由Spring API推断来的Kotlin类型考虑空安全需要`strict`值。但是应当在具备以下认识的情况下使用：Spring API可空性声明甚至会在小版本发布之间进化，并且可能会在未来加入更多的检查。

**警告** 泛型参数、可变参数和数组元素的可空性还不被支持。最新信息请查看[SPR-15942](https://jira.spring.io/browse/SPR-15942)。Spring Boot自己的API[也还没有被注解](https://github.com/spring-projects/spring-boot/issues/10712).
