@javaevolved: Spring Null Safety with JSpecify Spring 7 adopts JSpecify annotations, making non-null the default and reducing annota……
摘要
Spring 7 adopts JSpecify annotations, making non-null the default and simplifying null safety by reducing explicit annotations.
查看缓存全文
缓存时间: 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中的严格字段初始化已移至预览
JEP 539在JVM中引入了严格初始化的字段作为预览功能,要求字段在读取前必须初始化,以避免出现0或null等默认值。这为基于JVM的语言提供了更强的完整性保证。
零假设对照是必要但不充分的:评估科学发现与可解释性系统的验收防线
本文表明,仅依靠零假设对照不足以验证科学发现和可解释性系统,证明了像闭合性这样的验收测试可能被科学上毫无意义的候选结果通过,并提出结合多种标准,既能接受真实结果,又能拒绝零假设结果。
@namanambavi: https://x.com/namanambavi/status/2104931211818971359
Jev by TypeSafe 是企业AI的决策组件,提供类型化判断(Choice、Score、Noul)以替代工作流中的文本生成,从而实现更好的治理和验证。
是时候继续前进:无空值和袋子的查询
一篇学术论文,认为 SQL 使用空值和袋子是不必要的,并提出了完全规范化、无空值的语言 Rel,作为集合语义可以取代这些特性的证据。
@garrytan:用确定性代码替代 LLM 调用是显而易见的选择,而且还能产出更好的代码,更符合我们……
Garry Tan 分享了 AJ Asver 关于 Typesafe AI 的 Jev 框架的帖子,该框架将重复的 LLM 调用转换为确定性代码,据称通过使用 agentrun(),可将处理 100,000 条合规警报的成本从使用 Opus 5 时的超过 29 万美元降至不到 2.6 万美元。