Introduction to Spring (Setting Up Project)

In this tutorial, we are going to create a simple Spring Project. There are multiple ways to create a simple Spring Application. Here, I am using Spring Tool Suite for the implementation. Spring Tools Suite is the next generation of Spring tooling for your favorite coding environment. Largely rebuilt from scratch, it provides world-class support for developing Spring-based enterprise applications, whether you prefer Eclipse, Visual Studio Code, or Atom IDE. Here I’m using Spring Tools 4 for Eclipse for development purposes. Download here, extract the file and run “STS.exe”.

There are multiple ways to create a simple Spring project. But, the first approach we are going to try to create a Maven Project. So here are the steps.

Once Spring Tool Suite Open, click File-> New -> Maven Project

We are going to create a simple Maven project and add spring dependencies to it. Then choose to Create a simple project (skip archetype selection)

If you do not skip the archetype selection it basically lets you choose from one of the different templates of archetypes that come with Maven. For now, it is not necessary for us. Now click Next. Then you will get Advance selection options like this.

You should add a bunch of details to create this Maven Project. Group ID, Artifact ID and the version. The Group ID is something like a Package Name. It uniquely identifies the namespace for your project. You can use any meaningful name. I have used io.rst.springdemo as the Group ID. The Artifact ID is kind of like a Project Name. You can give any name. The version is basically the version of the component. Just leave the version section. Then click Finish.

Now what Maven is going to do is just download a simple template and create our project structure. There are a bunch of source folders and dependencies. But here we can see project already has one dependency like JRE System Library(J2SE-1.5).

You can see a file called pom.xml and this is where you can declare your dependencies. Open that file and just choose the pom.xml tab.

Now you can get a text representation of pom.xml which is clean XML.

Here are the details we already entered which are the Group ID, Artifact ID and the Version.

Now add this block of code to the pom.xml.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>

This is basically declaring that our project is a child of a parent project. That means you can have a Maven project which is a parent and another Maven project which is a child of that parent. The idea is that you can have the configuration defined in the parent project and as well as in the child project using inherit configurations. We are setting our project to be a child of project spring-boot-starter-parent.

Spring Boot Team created a project called spring-boot-starter-parent and they put all the default Maven Configurations into that project. So this one project called spring-boot-starter-parent contains that opinionated set of Maven Configurations. Now all you have to do is to declare that project as a parent and then your project contains same all configurations.

Actually, Maven serves for Dependency Management. It specifies the list of all the JAR files that we need and even downloads them and adds them to the classpath.

When we are creating a web Application with RESTfull API ’s, the application should listen to REST requests and provide REST responses. You may have a bunch of JARs to import in order to build a Spring Web Application. Then you should identify those RESTs and add each and every entry to this pom.xml.

Spring Boot says we know you are creating a web project and you need this list of Jar’s to add. Anyway, they create a meta dependency which is like a parent dependency which interns all the dependencies that you would need. All you need to do is establish one dependency to that meta dependency.

Add this block to your pom.xml.

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

Now the Group ID is org.springframework.boot and the Artifact ID is spring-boot-starter-web(The dependency that we are actually importing ). Now you do not need to add each and every dependency to the project because you have convenience dependency called spring-boot-starter-web. Now save the pom.xml then you can see the list of Maven Dependencies.

Now may have an Error like this.

Now right click on the project name and go to Maven then choose Update Project. Then the Project will update.

If you want the latest version of JDK(Let’s say I want Java SE 8 ). Use this link to check Java SE versions History. Add this code to your pom.xml.

<properties>
<java.version>8</java.version>
</properties>

Then, save pom.xml and you have to update Project again. Now you can see JRE System Library is been updated to 1.8.

So this is the basic configurations we should do to make a Spring Project. We’ll make a simple full Web Application in the next tutorial.

Thank you!

Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Software International