Spring 5 中的@Nullable 被注解为@Nonnull

@Nullable in Spring 5 is annotated with @Nonnull

我刚刚更新我的项目以使用 Spring Boot 2.0.3Spring Framework 5.0.7

现在我看到 MessageSource 方法中的某些参数使用 Spring 闪亮的新 @Nullable 注释进行了注释。但是由于这个注释,IDEA 14 说适当的参数不能是 null (SURPRISE!).

据我了解,这是因为 @Nullable 被注释为 @Nonnull:

@Nonnull(
    when = When.MAYBE
)

用逻辑上相反的标记标记注释的原因是什么?

正如@StephanHerrmann 所解释的,它与原始的 JSR 相关。 所以想法是在某些情况下允许 null 值 - 只是为了使代码自我记录。 javax.annotation.Nullable:

中实际使用了类似的方法
@Documented
@TypeQualifierNickname
@Nonnull(when = When.UNKNOWN)
@Retention(RetentionPolicy.RUNTIME)
public @interface Nullable {

}

在最新的 IDEA 版本中,支持(或将支持)此注释 out of the box。 要修复旧版本中的 IDEA 警告,您只需要 add the Spring's annotation manually.