Learn Spring Framework in 2020: Quick Start

Introduction

Spring is a java framework that provides a comprehensive programming and configuration design. It provides everything you need for creating a java enterprise application. Additionally, it also brings a lightweight structure that provides reusable and clean coding patterns

Spring framework can support other frameworks to provide flexible development for various types of applications. However, It is commonly used in web applications or mostly in the back-end web application. Since, it has a tremendous data access feature that can handle big data between the application and the database.

This tutorial will teach you how to set up and install the spring framework. And also teach you how to create your first web application using the spring framework

Spring boot framework

Spring framework provides a quick and easy way of setting up the spring projects or applications. Therefore, within minutes everything we need for the spring application like configuration, dependencies, and more will be automatically generated.

Spring boot is a spring-based framework that helps to accelerate spring application development, and it gives easy and stand-alone applications. Consequently, It helps the developer to focus more on the development side instead of focusing on spring infrastructure.

Requirements

  • Sprint requires java fundamentals knowledge.
  • Spring Framework 5.1 and above requires JDK 8 and above version (Java SE 8+)
  • Text Editor or IDE, but I recommend eclipse or Spring Tool Suite (STS)

Refer to this post for the java fundamentals and JDK installation:

Eclipse setup

  • Install JDK 1.8 into your local desktop, then followed by eclipse IDE. after the installation you can now open the Eclipse IDE.
  • Find Help menu, then click Eclipse Marketplace…
  • Search for the spring tool suite (STS), in the search bar type “STS” and look for the Spring Tool 4 like the image below, then click install:
  • Select all the other recommended features, then click confirm:
  • After the installation, Eclipse is requesting to restart the whole IDE, click restart to apply the change in eclipse IDE.
  • When the eclipse is restarted, go to file new menu, and look for Spring Starter Project, if you can’t see it you select and find Spring Boot folder to see the Spring Starter Project.
  • Complete the required fields like the image below, then click next:
  • Find Web, then select Spring Web and also select Spring Boot DevTool (We must select the dependencies based on the application needs. Therefore, spring boot does auto-configuration for us.):

Basic Web Application

After you followed the steps in the previous section, we can now proceed into our first Web application using the spring framework.

As you noticed, the generated project contains class, which is a starting point of the project:

@SpringBootApplication
public class QuickStartApplication {
	public static void main(String[] args) {
		SpringApplication.run(QuickStartApplication.class, args);
	}
}

@SpringBootApplication is used to enable the auto-configuration in the spring boot framework. It is equivalent to the @Configuration, @EnableAutoConfiguration and @ComponentScan annotations which will discuss in the next tutorial.

SpringApplication.run() method serves as a launcher of the application.

Launch the web application

Let’s try to launch the application. Open the spring boot dashboard to see the running application. Click the green icon to open the spring boot dashboard:

As you can see, the panel will be opened at the bottom of the eclipse IDE. Right-click the QuickStart application and then click the (Re)start menu to run the app. If you see a green up arrow, it means the app is successfully launched.

An alternative way is we can run the application using the command line, open cmd, and then go to the path where the app is located. After that type or copy and paste the script below:

mvnw spring-boot:run

You can check the application using a browser, just type localhost:8080 in the browser bar. 8080 is the default port of the spring application, however, you can change it when you have an issue with it.

Using property file you can change the port, go to the src/main/resources, and then open application.properties file. Copy and paste the new port server.port=8090

As of now, when we open the application in the browser. As of result, it will return an error or blank page:

The reason is, we didn’t add any request mapping yet. However, in the next section, we will discuss GET mapping that returns a “Hello world” message.

Print Hello World

Now let’s create another class for the controller of our application. The controller class is originally coming from the spring MVC framework. MVC means Model, View, and Controller. However, we will not discuss further topics about spring MVC. If you want more information you could wait for the spring MVC tutorial.

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

	@RequestMapping("/greetings")
	public String greetings() {
		return "Hello World";
	}

}

@RestController enables the spring MVC request mapping, as a result, spring MVC is ready to handle web requests. @RequestMapping is used to define a mapping request for web app. the first map we defined "/greetings" is for the greetings() method. When we invoke the map request using the browser, the web app will return pure text:

Conclusion

Using browsers is not accurate for testing spring web requests. Since browsers only interpret GET requests. There is a lot of tools available for the other requests like POST, PUT, DELETE, etc.. You can use Postman in google extension or curl(cmd tool). For further information about this topic, you could wait for the spring Restful tutorial.

For the next topic, you will learn about the spring architecture and IoC or Inversion of Control pattern.

One thought on “Learn Spring Framework in 2020: Quick Start

  • goyard bags
    August 9, 2020 at 5:27 pm

    An attention-grabbing discussion is value comment. I think that you need to write extra on this matter, it might not be a taboo topic however typically people are not sufficient to speak on such topics. To the next. Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

BCF Theme By aThemeArt - Proudly powered by WordPress.
BACK TO TOP