Basics and “Hello World” in Spring

 

Introduction to Spring Boot, Part 1 Basics and “Hello World” in Spring

Like Java, the Spring framework is now more of a part of the software outstaffing development establishment. Thanks to expansions such as Spring Boot, which we present in this series, it is still up to date.

Companies on the topic

Spring supports the developer, so the Spring Initializr helps with the creation of projects and the selection of the right components.Spring supports the developer, so the Spring Initializr helps with the creation of projects and the selection of the right components.

(Image: Spring.io)

With the rapid development of the” young savages ” such as Node.js can’t keep up with the Spring Framework – but it doesn’t have to. The Spring framework has a firm place in backend development, for example when accessing databases or programming rest APIs.

The core of Spring is a Dependency Injection Framework. In this, objects are no longer created by the developer with the “new”operator, but only described in a configuration. The instantiation of classes is carried out by the IOC (Inversion of Control) container, which in Spring is represented by the Application Context.

The advantage of this approach: hard dependencies in the Code are avoided, and testability of relief. Over the years Spring has grown and now much more than just this container. Spring Data, Spring MVC, Spring Security, Spring Cloud, Templates for JDBC or rest access, AOP, ORM, WebFlux – the list of features could be continued for a long time.

The most important expansion of the last years is called “Spring Boot” and has taken the developer hearts by storm. Spring Boot facilitates the tedious configuration through component scanning and auto-configuration, two powerful features that we will take a closer look at over the course of this series.

Collections of curated libraries, the so-called starters, do not have to deal with versions and dependencies when using existing software. Finally, the framework works with an embedded Tomcat server that starts developed services at lightning speed. All in all, this leads to a huge boost in productivity and, more importantly, it’s a lot of fun!

Initializr

The Spring-Initializr helps to create the project.The Spring-Initializr helps to create the project.

(Image: Dr. Koller / Spring)

To create a Spring boot project, the developer is provided with a generator called Spring Initializr. On the page you can select project properties such as programming language, spring boot version and the build system (Maven or Gradle). Also important libraries like in the screenshot “Spring Web” are selected with one click. The generated project is loaded in ZIP format and can be opened after unpacking in an IDE like the Spring Tool Suite (STS) using Maven import.

Tour through the project

The structure of the generated project corresponds to a Maven project.The structure of the generated project corresponds to a Maven project.

(Image: Dr. Koller / Spring)

The project structure corresponds to that of a common Maven project with the folders “src/main/java” and “src/main/ressources”. The most interesting file is the Maven project file pom.XML. It contains a reference to the Parent Pom “spring-boot-starter-parent”, from which it inherits many features. These include, for example, default configurations for Maven plugins and, again inherited from the parent “spring-boot-dependencies”, a lot of dependencies in matching versions.

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

In the dependencies, i.e. the integrated libraries, the feature “Spring Web” selected in the Initializr can be found, also in the form of a starter.

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

In addition, one comes across a dependency “spring-boot-starter-test” for creating unit tests and a plugin with which the service can be built as an executable jar:

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

In addition to the POM, the application class is also interesting. It contains the main method for starting the Java application. In this, the static method “SpringApplication.run”, which is responsible for bootstrap and start of the spring container.

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

Annotation in annotations

The @SpringBootApplication annotation is a collective annotation that summarizes three individual annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan. @Configuration marks the class as the configuration class. This means that beans can be defined for the application context in the class.

@EnableAutoConfiguration enables auto-configuration, a Spring Boot mechanism that closes to the intended use and automatically configures required beans from various features, such as the added libraries. Here, for example, the dispatcher servlet is configured as a front controller for incoming requests, because the POM contains the dependency to spring-boot-starter-web.

@ ComponentScan enables automatic scanning of defined parts of the classpath for Spring components. Components are specialized classes such as controllers or services. Classes marked as such are loaded into the Spring container without the need to configure a bean.

The other files are not as crucial. In ” application.properties ” properties such as the port used can be defined. “mvnw and mvnw.cmd ” are shell or Windows scripts that can be used to build the project without a complete Maven installation.

Hello Spring Boat

By selecting the “spring-boot-starter-web” starter, Spring Boot recognizes that a model view controller or MVC application is to be built here. An incoming request (for example, calling a page in the browser) is directed to the front controller of the application.

In Spring, this front controller is the aforementioned dispatcher Servlet. It takes over the request and uses a routing table to decide which method should edit the request. This routing table is still missing here, it is created by entries in a controller class.

So we create a controller class and annotate it with @ Controller. The class becomes a component. Since this example is about people, it is called “PersonController”. In the class, a method called “home” should edit the request to the path”/”. The call of the URL in the address bar of the browser leads to a GET request, so it is marked with the annotation “GetMapping”.

Analog mappings also exist for the other HTTP methods. Finally, Spring needs to be told that the return value of the method should be written to HttpResponse. This is done by the annotation @ResponseBody:

@Controller
public class PersonController {
   @GetMapping("https://www.dev-insider.de/")
@ResponseBody
public String home() {
return "Hello World";
}
}

The application can now be started by calling the Application class, it runs by default under port 8080. At startup, the integrated Tomcat server is automatically booted up, on the console you can track the associated messages.

A call to the URL http://localhost:8080 in the Browser, and eventually leads to the desired display of the “Hello World”. Not spectacular yet, but a first sign of life from Spring Boot! In the next part of the series we will look at Views with Thymeleaf.

(ID:46965374)

Ready to see us in action:

More To Explore

IWanta.tech
Logo
Enable registration in settings - general
Have any project in mind?

Contact us:

small_c_popup.png