-1

I am relatively new to Java, as a JS dev, working on a SpringBoot application I see a pattern that peaked my curiosity:

Within the Application.java there are several import classes, which are then marked exclude within @SpringBootApplication, eg

import org.springframework.boot.autoconfigure.data.database.DataBaseAutoConfiguration;
...
@SpringBootApplication(
    exclude = {DataBaseAutoConfiguration.class, ...}
)

DataBaseAutoConfiguration is not referenced anyplace else in the codebase, except here.

Can someone explain the purpose of this pattern? It feels odd to import the class then immediately exclude it in the configuration

Why not have something like:

exclude = {"DataBaseAutoConfiguration", ...} then lookup the class to ignore within Spring, avoiding the apparent "useless" import?

or:

// import nothing
@SpringBootApplication
Vinnie James
  • 5,763
  • 6
  • 43
  • 52
  • "Meaning of the import statement in a Java file" is off topic and not helpful to this question – Vinnie James Nov 15 '18 at 23:35
  • Please clarify. If you understand what `import` does, are you just asking what `SpringBootApplication`'s [`exclude`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html#exclude--) element does? – Sotirios Delimanolis Nov 15 '18 at 23:37
  • 1
    `exclude`'s type is `Class[]`, so it expects `Class` values, not `String` values like you suggest. There's an `excludeName` that takes a `String[]`, but you'd need to provide the fully qualified type name, so `excludeName = {"org.springframework.boot.autoconfigure.data.database.DataBaseAutoConfiguration"}`. Using the `Class` literal makes this more type safe. – Sotirios Delimanolis Nov 15 '18 at 23:41
  • I'm aware of how import works, and a bit how exclude works. The question is more how the two interact, it feels like `import thing...unimport thing` and stood out – Vinnie James Nov 15 '18 at 23:44
  • The `import` has no effect other than letting you use the simple name of the class. You can very well have had `exclude = {org.springframework.boot.autoconfigure.data.database.DataBaseAutoConfiguration.class}` with no `import` statement. The language feature is completely unrelated to an element of some annotation. – Sotirios Delimanolis Nov 16 '18 at 00:21

1 Answers1

5

tl;dr import is a Java language feature, exclude is a Spring Boot feature.

You have to import classes to reference them in your code. The exclude in this case is specific to Spring Boot and is simply instructing the spring context to not trigger any of the configuration beans inside of DataBaseAutoConfiguration. Technically speaking, exclude is a field inside of the @SpringBootApplication annotation.

The import is only required so you can reference DataBaseAutoConfiguration in the code. Without the import, you would get a compile error.

Mike
  • 4,722
  • 1
  • 27
  • 40
  • The reason I ask, is because the only reference to `DataBaseAutoConfiguration` is in the `import/exclude`. If `DataBaseAutoConfiguration` isnt used anywhere else, why the `import/exclude`? – Vinnie James Nov 15 '18 at 22:27
  • 2
    If you remove the `exclude = {DataBaseAutoConfiguration.class, ...}` then yes, you don't need the import statement. But this also means your app will load all of the configuration/beans from `DataBaseAutoConfiguration`, which may not be desirable for this particular app (i.e. if you have your own custom Database configuration class that handles creation of a Datasource, etc). – Mike Nov 15 '18 at 22:30
  • Is this because somewhere down the line Spring IS going to try to call `DataBaseAutoConfiguration` even though its not referenced again in the code. In this case it is imported, in order to reference here and tell Spring to ignore it when it comes up within Spring? – Vinnie James Nov 15 '18 at 22:31
  • 2
    Put another way: you need the import solely for the purpose of the compiler and JVM being able to find the definition of the `DataBaseAutoConfiguration` class to use in your code. You can't tell Spring Boot to exclude that class if the compiler and JVM don't even know how to load it. – Mike Nov 15 '18 at 22:32
  • 2
    Correct: presumably `DataBaseAutoConfiguration` is marked as `@Configuration` so without the exclude, Spring would scan it and load any beans and config from it. – Mike Nov 15 '18 at 22:33
  • Why not have something like `exclude = {"DataBaseAutoConfiguration", ...}` then lookup the class to ignore within Spring, avoiding the apparent "useless" import? – Vinnie James Nov 15 '18 at 22:34