wasAsked.

What is auto-configuration in Spring Boot and how does it work?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Java, Spring Boot

Community answers

Posted by wasAsked editorial collection

Auto-configuration is the Spring Boot feature that automatically creates and configures beans based on what is on the classpath, which beans you have already defined, and your application properties. It is the main reason Spring Boot applications need so little setup. Example Add spring-boot-starter-web and spring-boot-starter-data-jpa plus a PostgreSQL driver, and set: spring.datasource.url=jdbc:postgresql://localhost:5432/shop spring.datasource.username=app spring.datasource.password=${DB_PASSWORD} Without writing any configuration classes, Spring Boot creates: - An embedded Tomcat server and Spring MVC DispatcherServlet with Jackson JSON conversion. - A HikariCP DataSource from your properties. - An EntityManagerFactory with Hibernate, a transaction manager, and Spring Data JPA repository implementations. How it works 1. @SpringBootApplication combines @Configuration, @ComponentScan and @EnableAutoConfiguration. 2. @EnableAutoConfiguration imports candidate auto-configuration classes listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports files inside starter JARs (older versions used META-INF/spring.factories). 3. Each auto-configuration class is a normal @Configuration (annotated @AutoConfiguration) guarded by conditions, so it applies only when appropriate: - @ConditionalOnClass: a class is on the classpath (for example DataSource and a JDBC driver). - @ConditionalOnMissingBean: you have not defined your own bean of that type — your configuration always wins. - @ConditionalOnProperty: a property is set to a specific value. - @ConditionalOnBean, @ConditionalOnWebApplication, @ConditionalOnResource and others. 4. Properties are bound to type-safe classes with @ConfigurationProperties (for example DataSourceProperties for spring.datasource.*). Simplified example of an auto-configuration class: @AutoConfiguration @ConditionalOnClass(ObjectMapper.class) public class JacksonAutoConfiguration { @Bean @ConditionalOnMissingBean ObjectMapper objectMapper() { return new ObjectMapper(); } } Customising and overriding - Define your own bean: Spring Boot backs off because of @ConditionalOnMissingBean. - Change behaviour with properties (server.port=9090, spring.jpa.hibernate.ddl-auto=validate). - Exclude an auto-configuration: @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) or spring.autoconfigure.exclude. - Use customizer beans (for example Jackson2ObjectMapperBuilderCustomizer) to tweak auto-configured beans without replacing them. Debugging auto-configuration - Start with --debug (or debug=true) to print the conditions evaluation report showing which auto-configurations matched and why others did not. - Actuator's /actuator/conditions endpoint shows the same information at runtime. Writing your own auto-configuration Libraries can provide auto-configuration (often with a custom starter) so that adding the dependency configures beans automatically, following the same conditional patterns. Interview summary: "Spring Boot auto-configuration uses conditional configuration classes discovered from starters to create beans automatically based on the classpath, existing beans and properties. Your own beans and properties override the defaults, and the conditions report shows what was applied."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics