Skip to content

Disable ContextCustomizer implementations that only impacts the BeanRegistry in an AOT-optimized context #32422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
snicoll opened this issue Sep 19, 2022 · 2 comments
Assignees
Labels
theme: aot An issue related to Ahead-of-time processing type: task A general task
Milestone

Comments

@snicoll
Copy link
Member

snicoll commented Sep 19, 2022

Running a WebMvcTest in a native image fails with

Failures (1):
  JUnit Jupiter:DemoControllerTests
    ClassSource [className = 'com.example.demo.DemoControllerTests', filePosition = null]
    => java.lang.IllegalStateException: Unable to instantiate DeterminableImportSelector org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelector
       org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.instantiate(ImportsContextCustomizer.java:316)
       org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.determineImports(ImportsContextCustomizer.java:296)
       org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.determineImports(ImportsContextCustomizer.java:276)
       org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.<init>(ImportsContextCustomizer.java:236)
       org.springframework.boot.test.context.ImportsContextCustomizer.<init>(ImportsContextCustomizer.java:71)
       [...]
     Caused by: java.lang.NoSuchMethodException: org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelector.<init>()
       java.lang.Class.getConstructor0(DynamicHub.java:3585)
       java.lang.Class.getDeclaredConstructor(DynamicHub.java:2754)
       org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.instantiate(ImportsContextCustomizer.java:311)
       [...]

This can be reproduced using this sample. This code seems to be required to build a suitable MergedContextConfiguration but I wonder if it should be running at all since the context has already been fully prepared at this point. See also #32420

@snicoll snicoll added type: bug A general bug theme: aot An issue related to Ahead-of-time processing labels Sep 19, 2022
@snicoll snicoll added this to the 3.0.x milestone Sep 19, 2022
@snicoll
Copy link
Member Author

snicoll commented Sep 19, 2022

A note that Spring Native has an hard-coded list of ContextCustomizer implementations to ignore at runtime:

private static final Predicate<SpringFactory> CONTEXT_CUSTOMIZER_FACTORY = factoryEntry(
			"org.springframework.test.context.ContextCustomizerFactory",
			"org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory",
			"org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizerFactory",
			"org.springframework.boot.test.context.ImportsContextCustomizerFactory");

@snicoll snicoll changed the title Missing hints for ContextCustomizerKey ImportsContextCustomizerFactory should not be invoked at runtime in an AOT-optimized context Sep 19, 2022
@snicoll snicoll changed the title ImportsContextCustomizerFactory should not be invoked at runtime in an AOT-optimized context Disable ContextCustomizer implementations that only impacts the BeanRegistry in an AOT-optimized context Sep 20, 2022
@snicoll
Copy link
Member Author

snicoll commented Sep 20, 2022

So it turns out that disabling the 3 implementations above were enough to fix the sample. It's a good first step in the right direction but we should be discussing how to make that a bit more declarative/obvious. This should be addressed by #32262

@snicoll snicoll self-assigned this Sep 20, 2022
@snicoll snicoll added type: task A general task and removed type: bug A general bug labels Sep 20, 2022
@snicoll snicoll modified the milestones: 3.0.x, 3.0.0-M5 Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: aot An issue related to Ahead-of-time processing type: task A general task
Projects
None yet
Development

No branches or pull requests

1 participant