SPRING BOOT
It is time to deal with another of the fundamental frameworks that are available to us. The Spring Boot. Let’s look at the main features with slides.
Point two means that the applications we are going to create with this framework once we run the build will be ready to use, they will not need anything else to run.
The main features of Spring Boot are three. First of all simplified dependency management, the Spring Framework as we used it had one small flaw, we had to waste time searching for and configuring dependencies. If the correct dependency or version had not been chosen, our application would have had difficulty starting. With Spring Boot this problem is completely solved, although we will have to select dependencies, the most important aspect is that it is Spring Boot itself that will go and search the network for all connected dependencies.
AUTOMATIC CONFIGURATION
The other cornerstone is automatic configuration, there will be as we shall see a single configuration file. Finally, the third pillar, the simplified deployment, we do not need for example a web server as we have seen so far, Tomcat is integrated in the BUILD phase directly into the JAR file, inside this file we have all the dependencies and all the elements needed to run our application, we will have a single cross-platform file, it will be enough to have the JVM to run it.
LET’S CREATE OUR FIRST APP
The first step is to generate a new design. We have several options available to us. I will show you how to create a new project using Intellij IDEA as your code editor. Spring Tool Suite 4 or any editor you like is also fine. This is the video that illustrates how to create a Spring Boot project.
WEBAPP ANALYSIS AND ACTIVATION
Let us analyze the structure of a Spring Boot project.
- src/main/java Locates the source files. Inside we find the package with a class renamed to Application that has a single main method that is then the entrypoint of our application.
- src/main/resources inside there are two folders, static where we will put all the static files, Bootstrap, Javascript, images, css etc. The second folder is templates where we are going to put the thymeleaf files. Then we have a properties file, which is fundamental and allows us to enter a considerable amount of options.
- src/test/java Spring Boot projects are test-oriented, all Spring Boot applications use tests a lot.
- Let us now look at the pom.xml file.
Some of the dependencies are found in this file, particularly the ones we selected; however, the vast majority activated by the POM.XML are automatically downloaded by Spring Boot the moment the project is generated. All the dependencies that are needed to make it work are downloaded automatically. This is the big advantage; we do not have to be the ones to select them.
CREATING A WEB APPLICATION WITH SPRING BOOT
Let’s look at the very simple Controller class.
As for the view, it will no longer be a JSP view but an HTML view with minor differences. Navigate with your browser at http://localhost:8080.
FIRST UNIT TEST SPRING BOOT
As mentioned Spring Boot is oriented toward automated testing. Even with such a simple example we can create our Unit Test, let’s create the class HttpRequestTest.java in which I have commented all the code. The video shows how to perform the test and application.
Leave A Comment