Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

Show two ways to disable DataSourceAutoConfiguration in a Spring Boot application.

We can exclude it in code with @SpringBootApplication(exclude = DataSourceAutoConfiguration.class), or in configuration with the spring.autoconfigure.exclude property set to the class's fully qualified name. The property form can differ per environment or profile without a code change. Either way the JDBC dependency stays on the classpath, so if the application really needs no database, removing the dependency is often the better fix.

Background to this question

This question usually comes from a real moment: we add a starter that drags in JDBC, start the application, and it fails because no database URL is configured. Knowing how to switch off one auto-configuration, and knowing when not to, is basic Spring Boot hygiene. In this answer, we will recap why DataSourceAutoConfiguration activates, reproduce the startup failure, look at the two ways to disable it, compare them, and finish with the cleaner fix of removing the dependency.

1Why DataSourceAutoConfiguration Activates

Spring Boot's DataSourceAutoConfiguration is guarded by @ConditionalOnClass for DataSource and the embedded database type classes. So as soon as spring-jdbc is on the classpath, for example through spring-boot-starter-data-jpa or spring-boot-starter-jdbc, Boot tries to create a DataSource for us. If it finds an embedded database such as H2 it uses that; otherwise it needs spring.datasource.url.

Java
// application with spring-boot-starter-data-jpa and no database settings
// ***************************
// APPLICATION FAILED TO START
// ***************************
// Description:
// Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
// Reason: Failed to determine a suitable driver class

Notice that Boot is doing exactly what it promises. It sees JDBC on the classpath and assumes we want a database. The question is how to tell it we do not, at least for now.

2Way 1: Exclude It in the Annotation

The @SpringBootApplication annotation has an exclude attribute that takes auto-configuration classes. Anything listed there is removed from the candidates before conditions are evaluated.

Java
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; // Boot 3 package
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class ReportApplication {
public static void main(String[] args) {
SpringApplication.run(ReportApplication.class, args); // starts, no DataSource bean
}
}
// when the class may not be on the classpath, use the name instead:
// @SpringBootApplication(excludeName = "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration")

This is explicit and visible to anyone reading the main class, and the compiler checks the class name. The downside is that it applies everywhere the class is used, in every environment, and changing it needs a new build. One more detail: Spring Boot 4 splits auto-configuration into technology modules, and the JDBC classes moved to a new package, so check your version for the exact import.

When DataSource is excluded, the configurations that depend on it, such as the JPA and transaction manager ones for JDBC, usually back off too, because they are conditional on a DataSource bean. If they do not, we exclude them as well.

3Way 2: Exclude It With a Property

The same effect is available as configuration. The property spring.autoconfigure.exclude takes a list of fully qualified class names.

YAML
# application.yml
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
# or for one profile only, in application-offline.yml
# or on the command line:
# java -jar report.jar --spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

The reason this form is useful is that properties can come from anywhere in Boot's property order: a profile file, an environment variable (SPRING_AUTOCONFIGURE_EXCLUDE) or a command-line argument. So a service can run without a database in an offline demo profile or a test, and with one in production, from the same jar. But there is a catch: the class name is a plain string, so a typo silently does nothing. Also, a later property source replaces the list rather than adding to it, so if two places set the exclude property, only one list wins.

4Comparing the Two Ways

Both ways remove the configuration from the candidates in the same way, so the result at runtime is identical. The difference is who controls it and when it can change.

Annotation exclude vs property exclude
Aspect@SpringBootApplication(exclude)spring.autoconfigure.exclude
Where it livesMain class, in codeAny property source
Per environmentNo, same everywhereYes, per profile or variable
Checked by compilerYes (exclude), no (excludeName)No, a typo is silently ignored
Change needs a buildYesNo
Removes the jarNoNo

5The Better Fix: Remove the Dependency

Notice the last row of the table. Neither way removes spring-jdbc, the driver or Hikari from the classpath. They stay in the jar, in container images and in vulnerability scans, and other libraries may still react to them. If the application truly never talks to a database, the honest fix is to remove the starter that brings JDBC in, or exclude it from the library that pulls it in transitively.

So the rule of thumb is simple. If the application never needs a database, remove the dependency. If it needs one only in some environments, use the property per profile. If a library forces JDBC on us and we can never use it, the annotation is a clear, permanent statement in code.

6Follow-up questions

Can I exclude an auto-configuration in a test only?

Yes. In a test we can set the property with @SpringBootTest(properties = "spring.autoconfigure.exclude=...") or use @ImportAutoConfiguration and test slices that include only what they need. Slices such as @WebMvcTest already leave out DataSourceAutoConfiguration, so often nothing needs excluding.

How do I confirm the exclusion worked?

Run with --debug and look at the condition evaluation report: excluded classes are listed in their own Exclusions section. The conditions actuator endpoint shows the same list on a running service.

What is the difference between exclude and excludeName?

exclude takes Class objects, so the class must be on the compile classpath. excludeName takes strings, which is useful when the class may be absent, for example in a shared library that should not depend on JDBC just to exclude it.

7Conclusion

In this answer, we saw that DataSourceAutoConfiguration activates as soon as JDBC is on the classpath, and that we can disable it in two ways: the exclude attribute of @SpringBootApplication, which is explicit and compiler-checked, and the spring.autoconfigure.exclude property, which can differ per profile or environment without a code change. Neither removes the dependency, so when the application needs no database at all, removing the starter is the cleaner fix.

What to say in the interviewAbout 80 seconds aloud

Open with

There are two ways. In code, I put exclude = DataSourceAutoConfiguration.class on @SpringBootApplication, or excludeName with the fully qualified name if the class might not be on the classpath.

Then, if they want more

In configuration, I set spring.autoconfigure.exclude to the fully qualified class name, in application.yml, in a profile file or as an environment variable. The runtime result is the same: the class is removed from the candidates before its conditions are evaluated, and the configurations that need a DataSource back off with it. I choose between them by who should control it.

The annotation is the same in every environment and needs a build to change, but the compiler checks it. The property can switch per profile, for example an offline demo without a database, but a typo is silently ignored.

I would also point out that neither removes the dependency. spring-jdbc, the driver and Hikari stay in the jar and in the security scans. So if the service never needs a database, I would rather remove the starter that brings JDBC in, and keep exclusions for jars I am forced to carry.

To confirm the exclusion, I would check the Exclusions section of the conditions report.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Show two ways to disable DataSourceAutoConfiguration in a Spring Boot application.": https://scrivio-interview-library.netlify.app/spring-boot/auto-configuration/b4e34b0c2c/