Spring / Spring Boot 4 Basics Interview Questions
How does Spring Boot 4 auto-configuration work and what is the new @AutoConfiguration annotation?
Spring Boot's auto-configuration mechanism has been refined in version 4. The core concept remains: Spring Boot detects libraries on your classpath and automatically configures beans accordingly -- but the implementation is now spread across 70+ modules rather than a single monolithic JAR.
// Spring Boot 4 auto-configuration mechanics: // 1. Conditional annotations still drive auto-configuration: @AutoConfiguration @ConditionalOnClass(DataSource.class) @ConditionalOnMissingBean(DataSource.class) @EnableConfigurationProperties(DataSourceProperties.class) public class DataSourceAutoConfiguration { @Bean @ConditionalOnMissingBean public DataSource dataSource(DataSourceProperties props) { return props.initializeDataSourceBuilder().build(); } } // 2. Registration is now in META-INF/spring/ // org.springframework.boot.autoconfigure.AutoConfiguration.imports // (replaces META-INF/spring.factories from Spring Boot 2) // 3. @SpringBootApplication still enables component scanning // and auto-configuration: @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } // 4. Exclude specific auto-configuration: @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class NoDbApp { ... } // 5. Conditional annotations reference: // @ConditionalOnClass - class is on classpath // @ConditionalOnMissingBean - no bean of this type // @ConditionalOnProperty - property is set // @ConditionalOnWebApplication - is a web app // @ConditionalOnExpression - SpEL expression is true
Key change from Boot 3: the @AutoConfiguration annotation (introduced in Boot 2.7) is now the standard approach. It replaces the older pattern of annotating auto-configuration classes with plain @Configuration plus a @ConditionalOn*. The @AutoConfiguration annotation also sets proxyBeanMethods=false by default for better performance in Boot 4's modular world.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
