Live2D

spring官网在线学习文档翻译7

7. Null-safety(空-安全)

Although Java does not allow to express null-safety with its type system, Spring Framework now provides following annotations in the org.springframework.lang package to declare nullability of APIs and fields:

尽管Java不允许用它的类型系统表示空安全性,但是Spring Framework现在在org.springframework中提供了以下注释。声明api和字段为空的lang包:

  • @NonNull annotation where specific parameter, return value or field cannot be null (not needed on parameter and return value where @NonNullApi and @NonNullFields apply) .
    • @NonNull注释,其中特定的参数,返回值或字段不能为空(不需要参数和返回值@NonNullApi和@NonNullFields应用)。
  • @Nullable annotation where specific parameter, return value or field can be null.
    • @Nullable注释,其中特定参数、返回值或字段可以为空。
  • @NonNullApi annotation at package level declares non-null as the default behavior for parameters and return values.
    • 包级的@NonNullApi注释将non-null声明为参数和返回值的默认行为。
  • @NonNullFields annotation at package level declares non-null as the default behavior for fields.
    • 包级的@NonNullFields注释将non-null声明为字段的默认行为。

Spring Framework leverages itself these annotations, but they can also be used in any Spring based Java project to declare null-safe APIs and optionally null-safe fields. Generic type arguments, varargs and array elements nullability are not supported yet, but should be in an upcoming release, see SPR-15942 for up-to-date information. Nullability declaration are expected to be fine-tuned between Spring Framework release, including minor ones. Nullability of types used inside method bodies is outside of the scope of this feature.

Spring Framework利用了这些注释,但是它们也可以用于任何基于Spring的Java项目中,以声明空安全的api和可选的空安全字段。泛型类型参数、可变参数和数组元素可空性还不受支持,但在即将发布的版本中应该会支持,最新信息请参阅spring -15942。Nullability声明预计会在Spring框架发布版之间进行微调,包括一些次要版本。方法体中使用的类型的可空性不在此功能的范围内。

Libraries like Reactor or Spring Data provide null-safe APIs leveraging this feature.

像Reactor或Spring Data这样的库提供了利用此特性的空安全api。

7.1. Use cases(用例)

In addition to providing an explicit declaration for Spring Framework API nullability, these annotation can be used by IDE (such as IDEA or Eclipse) to provide useful warnings to Java developers related to null-safety in order to avoid NullPointerException at runtime.

除了为Spring Framework API NullPointerException提供显式声明外,IDE(如IDEA或Eclipse)还可以使用这些注释向与null安全相关的Java开发人员提供有用的警告,以避免在运行时出现NullPointerException。

They are also used to make Spring API null-safe in Kotlin projects since Kotlin natively supports null-safety. More details are available in Kotlin support documentation.

它们还用于使Kotlin项目中的Spring API为空安全,因为Kotlin本身就支持空安全。更多细节可在Kotlin支持文档中获得。

7.2. JSR 305 meta-annotations(JSR 305元注释)

Spring annotations are meta-annotated with JSR 305 annotations (a dormant but widely spread JSR). JSR 305 meta-annotations allows tooling vendors like IDEA or Kotlin to provide null-safety support in a generic way, without having to hard-code support for Spring annotations.

Spring注释使用JSR 305注释(一种休眠但广泛传播的JSR)进行元注释。JSR 305元注释允许IDEA或Kotlin等工具供应商以通用的方式提供空安全支持,而不需要对Spring注释进行硬编码支持。

It is not necessary nor recommended to add JSR 305 dependency in project classpath to take advantage of Spring null-safe API. Only projects like Spring-based libraries using null-safety annotations in their codebase should add com.google.code.findbugs:jsr305:3.0.2 with compileOnly Gradle configuration or Maven provided scope to avoid compile warnings.

没有必要也不建议在项目类路径中添加JSR 305依赖项来利用Spring空安全API。只有像基于spring的库这样的项目在其代码库中使用了null安全注释,才应该添加com.google.code.findbugs:jsr305:3.0.2,并使用compileOnly Gradle配置或Maven提供的作用域来避免编译警告。

后续戳我^^

posted @ 2020-09-29 10:51  六爻呈乾  阅读(122)  评论(0编辑  收藏  举报