Setup for Creating a Web Application with Spring Boot
We have seen the importance of spring-boot and the flexible configuration it offers in development. In this tutorial, we will setup the project using spring initializer (which generates the project setup for us).
Using the Spring Initializer tool
It helps in the quick setup of the spring-boot application that can be bootstrapped (quick run) easily. You can find this here: start.spring.io
The above snapshot is the typical layout of spring initializer which is hosted by PIVOTAL team. To generate the project of our choice, we need to enter the details such as Group and artifact (can be seen in the above snapshot) which is combined together to get the package name. The current version while writing this tutorial is 2.1.7 (2.1.7 is still the most stable version but you can opt for 2.2.5 or above version as well). We have selected the dependency management tool as MAVEN and language as Java for our web application.
We need to select dependencies required for our project from the dependencies section as displayed in the above figure. We can choose from the search suggestions displayed for us. For this project, we have selected the following list of dependencies.
h2 database (In-memory database)
We can add some more dependencies when required further. After selecting everything, click on the generate project button which downloads a ZIP file with the project in it.
Importing the Project:
The ZIP file needs to be unzipped to some folder and then we will import it as a maven project to your Eclipse IDE. It might take some time to resolve dependencies if you are importing a maven project for the first time.
The above figure displays the typical maven project structure which has been generated by spring initializer for us. It was unzipped and imported to eclipse as a Maven project.
In the above figure, src/main/java is the path where all the Java source code goes. Observe the package name which is a combination of groupid and artifact which we entered in spring initializer. The package src/main/resources has the application.properties file and src/test/java contains the classes which we need to run the unit test cases.
The pom.xml file is where dependency scripts are included which we selected while creating the project using spring initializer tool.
Steps to import the project into Eclipse:
We have to import after clicking the option of import under File option (top left) in eclipse as follows. Notice the figures 3.4, 3.5, 3.6 for further steps.
File => Import => Maven => Existing maven project
The figure 3.7 shows the project on our file system after unzipping the ZIP file from spring initializer.
Paste the root directory path of the project (directory where pom.xml resides as shown above) which needs to be imported and proceed to click on next option on the wizard (can be seen in figure 3.6).
It is responsible for adding dependencies to the project. For example, if we check the maven dependencies section under project we can see hibernate related JARs which are added because of spring-data-jpa dependency in the spring initializer.
Spring-boot inherits most of the configuration from the parent project called as spring-boot-starter-parent enabling the auto-configuration feature in spring-boot. It also depends on the JARs available in the class path. All the project metadata is available in pom.xml file.
Conclusion: In this tutorial, we have created a spring-boot maven project from spring initializer and imported it to eclipse IDE. We have also discussed about the project components and dependency management by pom.xml file in Maven.