Spring Boot Fundamentals

Spring Application Steps 1 through 7

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment details the necessary steps to create a Spring Application.

Keywords

  • spring
  • spring 5
  • java
  • spring context
  • interfaces
  • spring beans

About this video

Author(s)
Felipe Gutierrez
First online
22 May 2021
DOI
https://doi.org/10.1007/978-1-4842-7066-0_2
Online ISBN
978-1-4842-7066-0
Publisher
Apress
Copyright information
© Felipe Gutierrez 2021

Video Transcript

OK, let’s review our contacts app. I said seven steps in order to have a web application up and running, so let’s follow each one of them. So the first step is execute a maven command to create a structure, and this is the maven command that we need to actually execute in the command line. So you can open a terminal window and then execute these particular command.

You can pass on any tabs, or you can copy this. And again, if you have already the source code, this is not necessary. Just import a project, and that will be enough. But in this case, if you are wondering how I created the application or, in this case, the structure, this is the command that you need to use.

Then step two is to modify their pom by adding the following dependencies– the spring dependencies, the web dependencies, they’re in-memory database, in this case, h2, and one of the most and widely used UI frameworks, in this case, the family that is going to allow us to have debut of our contact applications. And of course, it’s important to know that we require these final name tag at the end of our pom.xml that has the contacts value. This is going to allow us to generate the WAR as our contacts-app.war file. So this is important.

And don’t worry too much. If you execute that from the maven command from the previous step, this is going to be generated automatically, but this is just in case you’re wondering if you want to generate just a name. If you remove these final name tags, normally in maven when you execute the package, a command is going to have the version appended to the name of, in this case, the application. So we don’t want to worry about that, so just add the final name tag here.

OK, step three, we’re going to create a contact class with the following properties, just basic properties– name, email, phone. We’re going to create also the necessary class for using the jdbc template class and do the code for all the contacts. Of course, we need to create a controller that is going to send all the information and, in this case, all the contacts model to the view. OK, so this is a step three.

And step four, we are going to add the necessary HTML, CSS, and JavaScript. And in order to make this simple, instead, I want to have these in a very look and feel. So in this case, we can use the webjars, and the locators, and the bootstrap, a well-known framework for CSS that allows you to set a UI. So this is the only thing that we require.

Then on step five, we need to modify the web.xml with the following content. First of all, normally, we’re going to have the servlet, and, in this case, we’re going to set the name as a dispatcher servlet. And this is the class that we’re going to call, and that’s the one that is going to be load on startup. So the dispatcher servlet, we’re going to see that this is the main spring Java class that allows to actually the web application to start and set up everything correctly into our environment.

And we’re going to see that once we see the code. Of course, we need– required to have the servlet mapping. So this dispatcher servlet needs to go to the whole kind of a context. And in this case, the main context, that’s just the forward slash.

Then step six, we need to create the dispatcher-servlet.xml, so it has to correspond with the name of our servlet. Remember on the previous step, the name was dispatcherServlet, and we need to append a dash servlet.xml in order to be dispatcherServlet-servlet and be recognized by this framework, where we are going to have different declarations, OK? And in this case, this is where you set up everything about the spring components.

Remember, we have our classes, and now we need to tell about the configuration. And this is where we set the configuration. And in this case, we’re required to have the component scan, annotation driven, the resources, the jdbc, and some beans. And again, don’t worry too much. I’m going to explain it step by step what we are doing, what is happening behind the scenes in order to for the spring framework to recognize every single one of these tag declarations that we are going to use.

And finally, in step seven, we need to package our contacts app just by doing a maven clean package. This is going to generate in the target directory the contact-app.war file. And the only thing we need to do is just deploy it. And in order to deploy it, we need to follow these command. We are going to do docker run –rm.

The port that we’re going to use, in this case, is going to start with 8080 8080. And then the volume, in this case, we’re passing the current path slash target where my .WAR file is located. Then the tomcat is use local tomcat web apps. That’s where normally you deploy your tomcat or your Java web applications, so that’s where normally you set all the WAR files.

And in this case, the only we’re doing is just mounting that volume into the web apps. And of course, then the name of the image, tomcat jdk openjdk slim. And again, don’t worry. I’ll explain it step by step. So let’s jump into the contacts app demo, and then we’ll come back just to do a summary of this particular segment.