Take a look at using Spring Data in Spring Boot with custom databases.
- [Instructors] Spring Boot's data support is rich and robust, and it's based on the highly successful Spring Data platform. As such, there is rich support for traditional RDBMS systems, as well as more modern NoSQL databases built right into the platform. Again, it's based entirely on the Spring Data project. As such, there's a lot of material on Spring Data out there, and we're not gonna get too deep into it. I do highly encourage you if you're not that familiar with Spring Data to look at Mary Ellen Bowman's course, Spring: Spring Data, here in the library.
Now, with Spring Boot and Spring Data the starters leverage a lot of the common defaults that as developers who've used Spring Data for a long time, we get kind of in this copy and paste mode because it's almost always the same. That's the opinions that Spring Boot is leveraging. Now, part of configuring a database in a Spring Data project is getting the database drivers correct. And when I say drivers, not just the driver classes, but also the URL, and the username and password, and everything that goes with building that data source.
When you include a DB technology you get a set of properties built that you can then populate so that Spring will auto-configure your database drivers for you. Those properties can be used to supply the credentials, the URLs, and the various other activities that you traditionally need to use to connect to that database and retrieve data from it. Now, certain databases such as H2 or SQLDB will auto configure an embedded database.
And that's great for POC work, or demo work, or this course, because that's exactly what we are gonna do. You also have the ability to leverage common scripts to prime that embedded database so that we can get data from the beginning, instead of having to figure out some way to put the data into our database. Now, I mentioned that the data sources get created with the project itself, and auto configured. And when properly configured you will get a DataSource bean that you can inject wherever you need to.
Now, one caveat to this is that you can only have one database auto configured. Any other database connections that you need to make you will need to manually configure. However, in a true microservices environment multiple databases are very rare. Now, that DataSource can be injected as needed. But Spring Boot will configure your repositories if you're using JPA, for instance, and inject that DataSource into them so that you don't ever have to actually mess with the DataSource bean.
And that, again, is the power of Spring Boot and it's opinionated auto configuration.
- Creating a Spring Boot application
- Configuring a Spring Boot app
- Leveraging profiles
- Packaging and running Spring Boot web apps
- Building a command-line application
- Using Spring Boot starters: Spring Data, Spring Security, and more
- Extending Spring Boot
- Using Spring Boot Actuator to monitor app health and other metrics