In case the EnableAutoConfiguration annotation is set in a parent class, that is not taken into account.
how to reproduce
To a new Spring Boot project, add the org.springframework.boot:spring-boot-starter-jdbc dependency, but no jdbc configuration.
In this state, the application fails to start, which is normal. To avoid that, we can use the EnableAutoConfiguration annotation in order to tell spring boot to ignore the dataSource autoconfiguration using this code:
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
This also works as intended. However, the issue is if that annotation is set on a parent class, in which case, the child class will not "see it".
I have put together this small demo app to demonstrate the issue: https://github.com/nucatus/enable-auto-config-demo
Spring boot version: 2.3.4.RELEASE
Comment From: wilkinsona
Thanks for the sample. Using @EnableAutoConfiguration on a test class is rather unusual. Is this a side-effect of creating the example, or is the problem that you're seeing specific to this usage? If it's a side-effect of creating the example, could you please update it to more closely match your actual usage?
Comment From: nucatus
Hello Andy and thank you for looking into it.
The issue is related to the classpath of our test infrastructure, where we import a library over which we have no control. That library has a dependency on spring-boot-starter-jdbc which pollutes our classpath. Of course, we could filter it out at the time the dependencies are resolved, but we thought to keep it, since we might needed in our test infrastructure after all. Until then, or at least for a good chunk of the tests, we wanted to disable that auto-configuration using the spring @EnableAutoConfiguration annotation for that. The common configuration of those tests classes is part of a generic class, that is inherited by the actual test classes. This is the place where we wanted to add that annotation too, since it is affecting all the test classes that inherit the generic test class.
Comment From: wilkinsona
Thanks for the additional information. Rather than using @EnableAutoConfiguration, I'd recommend using @TestPropertySource to set the spring.autoconfigure.exclude property. Its value is a comma-separate list of the fully-qualified class names of the auto-configurations that you want to exclude:
@TestPropertySource(properties = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration")
Does this meet your needs?
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: nucatus
Yes, this works. Although this kind of configuration was available to me as well, I thought the two are equivalent and inter-changeable.
Comment From: gavenkoa
Alternative exclusion an be archived via @ComponentScan(excludeFilters =...) or @ImportAutoConfiguration(exclude = ...).