Question 1
Q1A development team is building a library of custom Spring Boot auto-configurations. To ensure a custom configuration MyCustomAutoConfiguration is only activated when the ThirdPartyService.class is present on the classpath, and a bean of type DataSource has already been configured by another auto-configuration, which combination of annotations should be used on the configuration class?
Show answer & explanation
Correct answer: A
The correct approach is to use @ConditionalOnClass(ThirdPartyService.class) to check for the presence of the class on the classpath, and @ConditionalOnBean(DataSource.class) to ensure a DataSource bean is already present in the context before this configuration is applied. @Configuration is necessary to mark the class as a source of bean definitions.