From the course: Spring Data
Spring Boot Starter data JPA - Spring Tutorial
From the course: Spring Data
Spring Boot Starter data JPA
- [Instructor] Now that we have the exercise files, let's start refactoring it to use Spring Data JPA. A big boost comes from simply upgrading to Spring Boot. So what do we get? The first is a less complex dependency management model provided by the Spring Boot starter and Spring Boot parent settings in a new pom.xml. The second is a significant reduction in the persistent context configuration. So how do we do this? We will start by replacing the pom.xml with the smaller one that employs the Spring Boot parent and Spring Data JPA starter dependencies. Next, we will remove the configuration class and replace it with a Spring Boot application class. Finally, we will upgrade the JUnit tests to employ Spring Boot test features. So here's our code and I'm on branch oh 02_01 and I have another tab here which goes right to the GitHub repositories. And I'm on branch 02_01e and on the pom.xml under the university folder and I'm just going to copy this and go into this pom.xml and just replace it all. Okay, so we've replaced our POM and so now here we see that there's this, it's several less lines. We have a Spring Boot starter parent and we add the Spring Boot starter Data JPA and our H2 dependencies. So it's a lot smaller. The next step we're going to do is refactor the persistence DB config. Here's the class that has our configuration. So let's look at this class and how complicated it is. Of all the beans that we had to do is set up the component scanning and enable transaction management. Set up the entity manager factory, configure the data source to use H2 and the transaction manager and handling exceptions. And then set additional properties. Let's rename, right click and rename this to UniversityApplication.java. And we are going to remove these annotations and then remove all of this metadata that, and now we're going to make a main method here. So this will be our main method for our application. Instead of having to be a library that's included by another application. And then we're going to add this, SpringBootApplication annotation at the beginning of the class. So that replaces all of the configuration we have because Spring Data is smart enough to say, okay, we have an H2 dependency. It sets up all of that, those options for us that we had to manually do. So let's then go into our tests, files. And instead of this extends, we are just going to say SpringDataTest annotation and do that to all of our tests. So this is something part of Spring Data test. And then that's our factory methods, a helper class. So let's run one of these and I'm going to set a break point in here. So I'm going to run this test in debug mode and we'll see what happens whenever we run this as a Spring Boot test application. So what actually happens here, let me show the debug console, is, it starts up the entire Spring Boot application within the JVM of the JUnit test. And so anytime you start up a Spring Boot application, you typically see this Spring method. And as this runs we see that it will fetch a number of courses and there's no courses yet in the system. But then it'll show the number of staff and their names. And we see in the logs that the persistent context, that Entity Manager Factory has been built automatically for us. So all the configuration that we were doing manually now is done automatically. And now let's just finish the test, clicking play up here to continue. So just by pivoting to Spring Data JPA and Spring Boot, we are already seeing improvement in this project.