How did the @AutoConfiguration annotation change ordering and registration in Spring Boot 2.7+, and what are its before/after attributes?
answer
- @AutoConfiguration = @Configuration(proxyBeanMethods=false) + ordering attrs
- Attributes: before / beforeName / after / afterName
- New file: AutoConfiguration.imports (not spring.factories)
- Same AutoConfigurationSorter, same semantics
- proxyBeanMethods=false gotcha
basics
~10 s@AutoConfiguration (Boot 2.7+) is a meta-annotation bundling @Configuration(proxyBeanMethods=false). It has before, beforeName, after, afterName attributes so you set ordering inline instead of separate @AutoConfigureBefore/After annotations. Classes are now listed in AutoConfiguration.imports.
solid answer
~30 sSpring Boot 2.7 introduced the @AutoConfiguration meta-annotation, which combines @Configuration(proxyBeanMethods=false) with built-in ordering attributes: before, beforeName, after, afterName. So @AutoConfiguration(after = DataSourceAutoConfiguration.class) replaces stacking a separate @AutoConfigureAfter. It also came with a new registration mechanism: auto-config classes are now listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (one class per line) instead of the EnableAutoConfiguration key in META-INF/spring.factories, which is deprecated. You can still add @AutoConfigureOrder or the standalone @AutoConfigureBefore/@AutoConfigureAfter alongside it. The semantics of ordering are unchanged — same AutoConfigurationSorter, same alphabetical-then-order-then-before/after pipeline — only the ergonomics and registration file changed. In Boot 3.x this is the standard way to author auto-configurations.
code
java · 18 lines// src/main/java/... class
@AutoConfiguration(
after = { DataSourceAutoConfiguration.class },
before = { JpaRepositoriesAutoConfiguration.class }
)
public class MyStarterAutoConfiguration {
@Bean
@ConditionalOnMissingBean
MyService myService() { return new MyService(); }
}
/* Registration file (Spring Boot 2.7+):
src/main/resources/META-INF/spring/
org.springframework.boot.autoconfigure.AutoConfiguration.imports
# one fully-qualified class name per line
com.example.MyStarterAutoConfiguration
*/go deeper
Likely unaware of the 2.7 change.
Should know the imports file replaced spring.factories and the inline attributes exist.
Should explain that semantics are unchanged and the proxyBeanMethods implication.
Should guide starter migration off spring.factories and reason about proxyBeanMethods pitfalls in shared libraries.
**The @AutoConfiguration meta-annotation.** Added in Spring Boot 2.7, `@AutoConfiguration` is itself annotated with `@Configuration(proxyBeanMethods = false)` (bean methods are not CGLIB-proxied — the recommended mode for auto-config for speed/memory). It exposes ordering directly: - `before` — array of `Class<?>` this auto-config should be applied before. - `beforeName` — same, by string class name. - `after` — array of `Class<?>` this should be applied after. - `afterName` — same, by string. These are exact equivalents of `@AutoConfigureBefore`/`@AutoConfigureAfter` (`value`/`name`), just folded into one annotation so a class needs fewer stacked annotations. **New registration file.** Before 2.7, auto-config classes were registered under the `org.springframework.boot.autoconfigure.EnableAutoConfiguration` key in `META-INF/spring.factories`. Since 2.7 the canonical location is a plain text file: `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports`, listing one fully-qualified class name per line (comments with `#`). The old `spring.factories` entry still works but is deprecated and removed as the recommended path in Boot 3. Classes annotated with `@AutoConfiguration` should be listed here. **What did NOT change.** The ordering *algorithm* is identical. `AutoConfigurationSorter` still: sorts alphabetically by class name for a deterministic baseline, then applies `@AutoConfigureOrder`, then topologically applies before/after (whether those come from the annotation attributes or the standalone annotations). Determinism, the alphabetical fallback, and the low-value-first order semantics are unchanged. **Combining annotations.** You can mix: `@AutoConfiguration(after = X.class)` plus a separate `@AutoConfigureOrder(...)`. You can also still use the standalone `@AutoConfigureBefore`/`@AutoConfigureAfter` on a plain `@Configuration` class the old way, but new code should prefer `@AutoConfiguration` + the imports file. **Gotchas.** - Forgetting to add the class to `AutoConfiguration.imports` means it is never picked up as auto-config, and its ordering annotations are silently ignored. - `@AutoConfiguration` implies `proxyBeanMethods = false`; if your `@Bean` methods call each other expecting a shared singleton via the proxy, that pattern won't work — inject the dependency as a method parameter instead. - Mixing `spring.factories` (old key) and the new imports file for the same classes can cause double registration confusion; migrate fully. - The `before`/`after` targets must be actual auto-configuration classes, or they have no effect. **When to use.** Any new starter or library on Boot 2.7+/3.x: annotate with `@AutoConfiguration`, set `before`/`after` inline as needed, and list the class in the imports file.
- Where do you register an @AutoConfiguration class in Spring Boot 3?In META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports, one fully-qualified class name per line. The old EnableAutoConfiguration key in spring.factories is deprecated.
- Does @AutoConfiguration change the ordering algorithm compared to the old annotations?No. It only folds the before/after configuration into one annotation and sets proxyBeanMethods=false. AutoConfigurationSorter behaves identically: alphabetical baseline, then @AutoConfigureOrder, then topological before/after.
saying these in an interview costs you the question
- Thinking @AutoConfiguration introduced new ordering semantics rather than just ergonomics + a new registration file
- Registering @AutoConfiguration classes only in spring.factories on Boot 3 and expecting them to be picked up as the recommended path
- Forgetting @AutoConfiguration implies proxyBeanMethods=false