skip to content

Mechanically, what is a slice annotation like @DataJpaTest composed of? Name the two ingredients that make slicing work.

level: middleimportance: must knowfreq 62%

answer

  1. TypeExcludeFilter = which beans
  2. AutoConfigure… = which auto-config
  3. @OverrideAutoConfiguration(enabled=false) → default OFF
  4. @ImportAutoConfiguration reads curated list
  5. composite meta-annotation

basics

~10 s

A slice annotation is a composed meta-annotation. It combines a @TypeExcludeFilter (to filter which beans get scanned) with one or more @AutoConfigure... annotations (to enable only that layer's auto-configuration).

solid answer

~40 s

Each slice is a composite annotation. Look at @DataJpaTest's source and you'll see: (1) @BootstrapWith(...) to bootstrap the test context, (2) a @TypeExcludeFilter (e.g. DataJpaTypeExcludeFilter) that restricts component scanning so only the slice's stereotypes are registered and general @Component/@Service beans are filtered out, and (3) a stack of @AutoConfigure... meta-annotations (@AutoConfigureCache, @AutoConfigureDataJpa, @AutoConfigureTestDatabase, @AutoConfigureTestEntityManager) that turn on just the auto-configuration classes relevant to JPA. The @AutoConfigure... annotations, via ImportAutoConfiguration, read a curated list of auto-config classes from spring.factories / META-INF spring/…AutoConfiguration.imports rather than loading Boot's entire auto-config chain. Together the TypeExcludeFilter (what beans) plus the AutoConfigure imports (what infrastructure) produce the minimal context.

code

java · 15 lines
java
// Sketch of what @DataJpaTest is composed of (real Boot source, trimmed)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@BootstrapWith(DataJpaTestContextBootstrapper.class)
@ExtendWith(SpringExtension.class)
@OverrideAutoConfiguration(enabled = false)      // blanket auto-config OFF
@TypeExcludeFilters(DataJpaTypeExcludeFilter.class) // bean-scan gate
@Transactional
@AutoConfigureCache               // each of these is meta-annotated with
@AutoConfigureDataJpa             // @ImportAutoConfiguration, pulling a
@AutoConfigureTestDatabase        // curated list of auto-config classes
@AutoConfigureTestEntityManager
public @interface DataJpaTest {
    // ...
}

go deeper

for a junior

Enough to say a slice is a composed annotation combining a scan filter and layer-specific auto-config.

for a middle

Name @TypeExcludeFilter and @AutoConfigureXxx as the two ingredients and explain what each restricts.

for a senior

Explain @OverrideAutoConfiguration(enabled=false) as the default-off pivot and how @ImportAutoConfiguration sources curated lists.

for a principal

Reason about maintaining custom slices, version drift in curated import lists, and context-cache implications of composition.

## The two ingredients Every Spring Boot slice annotation is a **composed (meta-)annotation**. Slicing is achieved by two independent mechanisms bolted together: ### 1. `@TypeExcludeFilter` — controls *which application beans* get scanned `@TypeExcludeFilter` is a Spring Boot annotation that registers a `TypeExcludeFilter` bean — a special `TypeFilter` consulted during component scanning. Each slice ships a dedicated subclass, e.g. `DataJpaTypeExcludeFilter`, `WebMvcTypeExcludeFilter`, `JsonTypeExcludeFilter`. Its job: when the test's `@ComponentScan` runs, **exclude every component that is not part of this slice**. So `@WebMvcTest` keeps `@Controller`/`@ControllerAdvice`/`Converter`/`Filter` and drops `@Service`/`@Repository`/`@Component`. If you pass explicit classes (`@WebMvcTest(FooController.class)`) the filter narrows further to just those. ### 2. `@AutoConfigure…` composites — control *which auto-configuration* is enabled Instead of Boot's normal 'enable ALL matching auto-configuration', a slice enables a **hand-picked subset**. This is done with layered `@AutoConfigureXxx` annotations, each of which is itself meta-annotated with `@ImportAutoConfiguration`. `@ImportAutoConfiguration` looks up the list of auto-configuration classes to import from a properties key in `META-INF/spring.factories` (Boot ≤2.x) or, in modern Boot, from `META-INF/spring/<AnnotationClassName>.imports` files. So `@AutoConfigureDataJpa` maps to a fixed list like `HibernateJpaAutoConfiguration`, `DataSourceAutoConfiguration`, `TransactionAutoConfiguration`, etc. ### Putting it together — @DataJpaTest ```java @Target(TYPE) @Retention(RUNTIME) @BootstrapWith(DataJpaTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) // turn OFF blanket auto-config @TypeExcludeFilters(DataJpaTypeExcludeFilter.class) @Transactional @AutoConfigureCache @AutoConfigureDataJpa @AutoConfigureTestDatabase @AutoConfigureTestEntityManager public @interface DataJpaTest { ... } ``` Note `@OverrideAutoConfiguration(enabled = false)`: this **disables Boot's normal `@EnableAutoConfiguration`** so that *nothing* is auto-configured by default — then the `@AutoConfigure…` imports add back exactly the slice's subset. That is the crux of slicing: **default OFF, then opt specific auto-configs back in.** ## Key classes/annotations to remember - `@TypeExcludeFilters` / `TypeExcludeFilter` — the bean-scan gate. - `@AutoConfigureXxx` — layer-specific enablers (each meta-annotated with `@ImportAutoConfiguration`). - `@ImportAutoConfiguration` — reads the curated auto-config list. - `@OverrideAutoConfiguration(enabled=false)` — kills blanket auto-config so the slice is minimal. - `@BootstrapWith(...TestContextBootstrapper)` — wires the slice into the TestContext framework. ## Gotchas - Because auto-config is default-off, an auto-config your test needs but the slice doesn't include simply won't run — you add it back with `@AutoConfigureXxx` or `@ImportAutoConfiguration`. - `@TypeExcludeFilter` filtering means a stray `@Component` you rely on is silently absent (unsatisfied dependency at wiring time). - The curated import lists are versioned with Boot — don't assume a class is included; check the slice's `@AutoConfigure…` chain.

  • What does @OverrideAutoConfiguration(enabled=false) do inside a slice?
    It disables Boot's normal blanket @EnableAutoConfiguration so nothing is auto-configured by default. The slice then re-enables only its curated subset via the @AutoConfigureXxx imports, keeping the context minimal.
  • Where does @ImportAutoConfiguration get its list of classes from?
    From a key in META-INF/spring.factories (older Boot) or a META-INF/spring/<AnnotationFQN>.imports file (modern Boot). It's a fixed, curated list per @AutoConfigure annotation — not dynamic condition matching over everything.

saying these in an interview costs you the question

  • Saying a slice just component-scans a package differently, ignoring the auto-config subset mechanism
  • Thinking slices still run full @EnableAutoConfiguration (they set it to disabled)
  • Confusing @TypeExcludeFilter (bean scan) with @AutoConfigure (infrastructure) — they are two separate gears

context