@javaevolved: Spring Null Safety with JSpecify Spring 7 adopts JSpecify annotations, making non-null the default and reducing annota……

X AI KOLs Timeline 工具

摘要

Spring 7 adopts JSpecify annotations, making non-null the default and simplifying null safety by reducing explicit annotations.

Spring Null Safety with JSpecify Spring 7 adopts JSpecify annotations, making non-null the default and reducing annota… Spring @NonNull/@Nullable → JSpecify @NullMarked (JDK 17+) https://javaevolved.github.io/enterprise/spring-null-safety-jspecify.html… #Java #JavaEvolved
查看原文
查看缓存全文

缓存时间: 2026/08/13 21:25

Spring Null Safety with JSpecify Spring 7 adopts JSpecify annotations, making non-null the default and reducing annota… Spring @NonNull/@Nullable → JSpecify @NullMarked (JDK 17+) https://javaevolved.github.io/enterprise/spring-null-safety-jspecify.html… #Java #JavaEvolved


Spring Null Safety with JSpecify

Source: https://javaevolved.github.io/enterprise/spring-null-safety-jspecify.html Code Comparison

import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

public class UserService {

    @Nullable
    public User findById(@NonNull String id) {
        return repository.findById(id).orElse(null);
    }

    @NonNull
    public List<User> findAll() {
        return repository.findAll();
    }

    @NonNull
    public User save(@NonNull User user) {
        return repository.save(user);
    }
}
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public class UserService {

    public @Nullable User findById(String id) {
        return repository.findById(id).orElse(null);
    }

    public List<User> findAll() {
        return repository.findAll();
    }

    public User save(User user) {
        return repository.save(user);
    }
}

Why the modern way wins

✂️

Non-null by default

@NullMarked makes all unannotated types non-null, so only nullable exceptions need annotation.

🌐

Ecosystem standard

JSpecify annotations are a cross-framework standard recognized by NullAway, Error Prone, and IDEs.

🔍

Richer tooling

Modern static analyzers understand JSpecify’s null model and report violations at compile time.

Old Approach

Spring @NonNull/@Nullable

Modern Approach

JSpecify @NullMarked

JDK Support

Spring Null Safety with JSpecify

Available

Available since Spring Framework 7.0 (requires Java 17+)

How it works

Spring 5 and 6 introduced their own null safety annotations in the `org.springframework.lang` package. While useful, these were framework-specific and required annotating every non-null element explicitly. Spring 7 migrates to JSpecify, a cross-ecosystem standard for null safety. The `@NullMarked` annotation at the class or package level declares that all unannotated types are non-null by default. Only actual nullable types need the `@Nullable` annotation, dramatically reducing verbosity. JSpecify annotations are recognized by major static analysis tools such as NullAway, Error Prone, and IntelliJ IDEA, bringing richer tooling support beyond what Spring-specific annotations provided.

Related Documentation

Proof

相似文章

JEP 539:JVM中的严格字段初始化已移至预览

Hacker News Top

JEP 539在JVM中引入了严格初始化的字段作为预览功能,要求字段在读取前必须初始化,以避免出现0或null等默认值。这为基于JVM的语言提供了更强的完整性保证。

稳定Rust的Never类型

Hacker News Top

Rust在经过两年多的开发后,稳定了其'never'类型,这一特性使得泛型代码更高效,并简化了语言中的类型推断。

假设弱化性质

Hillel Wayne — Computer Things

本文探讨了为什么在规范或测试中添加假设会从逻辑上弱化所得性质,使用了逻辑蕴含以及来自形式化方法和 Rust 的示例。此外,还讨论了尽管存在这种弱化,仍使用假设的实际原因。