Card 05/ 06
Practice: Predicting What Auto-Configuration Creates
Three setups, three answers, each decided by a condition you can name.
Auto-configuration creates a bean only when its conditions hold, mostly @ConditionalOnClass (a library is on the classpath) and @ConditionalOnMissingBean (you have not defined that bean yourself). For each setup below, say whether Boot creates the bean, and which condition decides it.
spring-boot-starter-data-jpa(the dependency that brings in JPA and Hibernate) is on the classpath, and you have defined your ownEntityManagerFactorybean.spring-boot-starter-cacheis on the classpath, you have added@EnableCachingyourself, and no cache library such as Caffeine is present.- Nothing web-related is on the classpath at all, and your main class is annotated
@SpringBootApplication.
See the answers
- No.
@ConditionalOnMissingBeansees yourEntityManagerFactoryand backs off, exactly as it does for aDataSourceyou define yourself. - Yes, a simple one. With no cache library on the classpath, Boot falls back to its own basic cache backed by a
ConcurrentHashMap. The@ConditionalOnClasscheck for Caffeine fails, but caching itself was still switched on. - No web server and no
DispatcherServlet, the component that routes each web request to a controller.@ConditionalOnClasslooks for Spring MVC and an embedded server; neither is on the classpath, so none of the web auto-configuration activates.
← → move through sections and cards