Card 05/ 06

ExerciseDifficulty: Advanced1 min

Will Boot Create It?

Three scenarios. For each, say whether Spring Boot's auto-configuration creates a bean, and why.

  1. spring-boot-starter-data-jpa is on the classpath, and you've defined your own EntityManagerFactory bean.
  2. spring-boot-starter-cache is on the classpath, and you've added @EnableCaching yourself, with no cache-provider library present.
  3. Nothing web-related is on the classpath at all, and your main class is annotated @SpringBootApplication.
See the answers
  1. No. @ConditionalOnMissingBean sees your EntityManagerFactory and backs off, the same as the DataSource gotcha.
  2. Yes, but a simple one. With no real provider on the classpath, Boot's own fallback — a basic ConcurrentHashMap-backed cache — is what gets created. @ConditionalOnClass for something like Caffeine fails, but caching itself was still asked for.
  3. No embedded server, no DispatcherServlet. @ConditionalOnClass checks for Spring MVC and an embedded server implementation; neither is on the classpath, so that whole family of auto-configuration classes never activates.