spring-projects / spring-projects/spring-framework
Custom ConstraintValidator injects null in native-image
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 60.2k
- Forks
- 38.8k
- Avg merge
- 5d 2h
- Merged PRs (30d)
- 27
Description
demo project link: https://github.com/mingchiuli/demo
public class AnnoValidator implements ConstraintValidator<Anno, Pojo> {
@Resource
private TestComponent testComponent;
@Override
public boolean isValid(Pojo value, ConstraintValidatorContext context) {
testComponent.call();
return true;
}
}
In Spring boot 3.3.6, I found if I injected a component in a implement Class of ConstraintValidator in JVM mode, it worked normal:
(console output:)
123
test
But It would generated a NullPointerExeception in GraalVM's native-image :
2024-12-12T12:46:54.279+08:00 ERROR 36733 --- [demo] [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: jakarta.validation.ValidationException: HV000028: Unexpected exception during isValid call.] with root cause
java.lang.NullPointerException: null
at com.example.demo.pkg.AnnoValidator.isValid(AnnoValidator.java:14) ~[demo:na]
at com.example.demo.pkg.AnnoValidator.isValid(AnnoValidator.java:7) ~[demo:na]
It seems the TestComponent can't be injected in native-image mode
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked demo project and AnnoValidator.isValid, then compare ConstraintValidator injection in JVM mode with the GraalVM native-image build. Reproduce the NullPointerException and trace why TestComponent is null; done when the validator receives its injected component in native-image mode without breaking JVM behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100