Repaso
EspaƱol
Spring, JPA & Java WebIntermediateSPR-11

What does Spring Boot auto-configuration do?

Options

  1. It rewrites your code automatically while compiling
  2. It reads the classpath and registers default beans (Correct option)
  3. It downloads any missing libraries while running
  4. It formats your code to match the Spring style

Answer

It inspects the classpath and your properties and registers sensible beans automatically (e.g. a DataSource if a JDBC driver is present), backing off if you define your own

Why

Auto-configuration classes are conditional: "if class X is on the classpath and no bean of type Y exists, create Y".

Example: add spring-boot-starter-data-jpa and a PostgreSQL driver, set the URL in properties, and Spring Boot creates the DataSource, EntityManagerFactory and transaction manager for you.

If you define your own bean of that type, the auto-configured one backs off. To see what happened, run with --debug.

Remember

Auto-config = conditional defaults that back off when you provide your own.

Key terms

Practice this question in Repaso