2

In a spring test there was the question: "Would Spring Data JDBC dependency autoconfigure a Datasource and a JDBCTemplate?"

I would say no because, you need a database connector dependency like h2 or oracle right to autoconfigure a datasource? And a JDBCTemplate needs also always a datasource?

Ringo777
  • 97
  • 7

1 Answers1

0

I suspect what you think is correct, also the wording is a litte off.

Strictly speaking Spring Data JDBC doesn't do any autoconfiguration at all.

Autoconfiguration is done by Spring Boot in cooperation with the Spring Boot Spring Data JDBC starter which depends on (i.e. includes the auotconfiguration done by the Spring Boot Starter JDBC

And that's the one actually autoconfiguring a Datasource and if a Datasource is available also provides/configures a JdbcTemplate.

In order to be able to configure a Datasource at the very least a JDBC Driver is required. Depending on the environment (is it a in memory database or not, are you in a test execution or not) possibly also some configuration properties like a JDBC URL is required as well.

If you want to understand better what the conditions are for autoconfiguration I recommend the list of Spring Boot autoconfiguration classes. If you search that list for e.g. "datasource" you'll find the relevant classes and links to their source code and annotations which I think are somewhat understandable even when you don't yet understand how Spring Boot works internally exactly.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348