-
Notifications
You must be signed in to change notification settings - Fork 40.7k
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
ApplicationContextRunner has inconsistent behaviour with duplicate auto-configuration class names #17963
Comments
- Don’t try manually adding the health indicator for OGM, but run after Neo4jHealthIndicatorAutoConfiguration.class. - Also rename the configuration (See spring-projects/spring-boot#17963).
I wonder if this might also be related to |
At least I ran into the problem of not being allowed to overwrite beans because my Test with faulty. Error was in my config, but I didn’t spot it. Happens easily with the behavior of the test runner. |
The problem is that The net result is that the framework does something different. The |
I've tried a few things and I am not sure how to tackle this problem. The context is created manually with a list of configuration classes that I know at runtime so I can't really use an @jhoeller do you have an idea for us? I've tried to use a |
I've been looking at fixing the issue (see snicoll@035cdda). The idea is to mimic what There is a timing issue that's a bit odd but I might have nailed it. When the auto-configuration backs off because a condition with You can reproduce by running With the change it fails with
Everything works as expected as far as I can see if we replace the |
I may have a fix for this based on @snicoll's initial work. Framework's condition evaluation is pretty tightly coupled to configuration class processing and an
We can overcome this thanks to the
Switching to an
I'd like to see what the rest of the team think about the use of the |
Hah, that's a neat trick, I like it. |
I'm wondering if Class<?>[] classes = Configurations.getClasses(this.runnerConfiguration.configurations);
if (classes.length > 0) {
BeanDefinitionRegistry registry = getBeanDefinitionRegistry(context);
ConfigurableEnvironment environment = context.getEnvironment();
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(registry, environment);
for (Class<?> configurationClass : classes) {
reader.registerBean(configurationClass, configurationClass.getName());
}
} |
Thanks for the suggestion @nosan. I've just pushed something that's a bit of an amalgamation of all the ideas suggested in the issue. It's too risky for earlier versions, but I think it's safe enough for 3.4.0. It was hard to create individual commits, so I've just added us all as co-authors. I hope that's OK with everyone. |
Given the following configurations:
and in another package, under the same, simple name:
Spring Framework refuses to work with two configuration classes having the same bean name (
someConfig
). This is fine, the error is usable.However, the
ApplicationContextRunner
does not do this and takes in the following configurationAnd fails the test by silently ignoring or overwriting the first configuration given.
While it would have been noticed in this case, you don't notice it when you test for the absence of one bean and the presence of another. As one config has maybe silently dropped, conditions are not tested.
This happens also with overlapping autoconfigurations having the same class name and this is where I actually found the issue:
Spring Boot and or Spring Framework do apparently support autoconfigurations with the same simple class name.
Given this bean
and Spring Factories containing
I can test my application:
However, the
ApplicationContextRunner
disagrees andfails.
While the duplicate names need good reasons and are of course a thing to debate, the application context runner should agree during tests with the framework to prevent errors in custom starters or applications, depending on what is actually tested.
I have attached the demo project.
autoconfigissue.zip
The text was updated successfully, but these errors were encountered: