Spring Boot Auto-Configuration
Spring Boot auto-configuration, @SpringBootApplication, starters, conditional beans, spring.factories
1What is auto-configuration in Spring Boot?
What is auto-configuration in Spring Boot?
回答
Auto-configuration automatically configures Spring beans based on dependencies present in the classpath. For example, if spring-boot-starter-data-jpa is present, Spring Boot automatically configures a DataSource, EntityManagerFactory, and TransactionManager. This significantly reduces the manual configuration required.
2Which annotation enables auto-configuration in Spring Boot?
Which annotation enables auto-configuration in Spring Boot?
回答
@EnableAutoConfiguration enables the Spring Boot auto-configuration mechanism. This annotation is included in @SpringBootApplication which combines @Configuration, @EnableAutoConfiguration, and @ComponentScan. It scans the classpath to detect auto-configuration classes.
3What does the @SpringBootApplication annotation combine?
What does the @SpringBootApplication annotation combine?
回答
@SpringBootApplication is a composite annotation that combines three essential annotations: @Configuration (declares a configuration class), @EnableAutoConfiguration (enables auto-configuration), and @ComponentScan (scans components in current package). This avoids declaring these three annotations separately.
How to exclude a specific auto-configuration?
What is the purpose of @ConditionalOnClass?
+17 面接問題