JSpecify and Spring Null-safety
Null-safety turns hidden runtime failures into visible compile-time guarantees
Spring Framework 7.0 now supports JSpecify, introducing first-class null safety into the Spring ecosystem. This means Spring can understand nullability contracts and help you detect potential NullPointerException issues at compile time, not in production.
What Is Null-safety?
Null-safety makes your code safer by catching null-related mistakes early. It creates clear expectations:
What can be null
What cannot be null
What APIs require strict checks
This leads to fewer runtime surprises and more reliable applications.
What Is JSpecify?
JSpecify defines standard annotations such as:
@NonNull@Nullable@NullMarked
Spring 7 understands these annotations and applies null-safety rules consistently across your beans and application logic.
📘 JSpecify guide: https://jspecify.dev/docs/user-guide/
📘 Spring null-safety docs: https://docs.spring.io/spring-framework/reference/core/null-safety.html
How to Enable Null-safety Checks (Maven)
To validate null-safety automatically, we use NullAway, Uber’s fast, compile-time checker, together with Error Prone:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>8</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-XDcompilePolicy=simple</arg>
<arg>--should-stop=ifError=FLOW</arg>
<arg>-Xplugin:ErrorProne
-XepOpt:NullAway:AnnotatedPackages=com.bomfim
-Xep:NullAway:ERROR
-XepExcludedPaths:.*/src/test/java/.*
</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.42.0</version>
</path>
<path>
<groupId>com.uber.nullaway</groupId>
<artifactId>nullaway</artifactId>
<version>0.12.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
What this setup gives you
Compile-time null-safety enforcement
Clear errors when annotations are violated
Automatic protection against accidental
nullvalues
More details:
📘 NullAway JSpecify Support: https://github.com/uber/NullAway/wiki/JSpecify-Support
You can access the code here: https://github.com/ThiagoBfim/spring-news
Using Gradle?
This talk explains it perfectly:
🎥 Null Safety in Java with JSpecify and NullAway — Sébastien Deleuze (Spring I/O 2025)
Conclusion
With Spring 7 + JSpecify + NullAway, Java finally gets a practical, lightweight approach to null-safety. You get earlier warnings, clearer APIs, and fewer runtime surprises, all with minimal setup.
If you found this useful, subscribe for free and hit like, more Spring & Java tips coming every week 🚀


