Enterprise Średniozaawansowany

Spring 7 adopts JSpecify annotations, making non-null the default and reducing annotation noise.

✕ Spring 5/6
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);
    }
}
✓ Spring 7
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);
    }
}
Widzisz problem z tym kodem? Daj nam znać.
✂️

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.

Stare podejście
Spring @NonNull/@Nullable
Nowoczesne podejście
JSpecify @NullMarked
Od JDK
17
Poziom trudności
Średniozaawansowany
Spring Null Safety with JSpecify
Dostępne

Available since Spring Framework 7.0 (requires Java 17+)

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.

Udostępnij 𝕏 🦋 in