Spring Boot 3.1 Tutorial: New features

Spring Boot 3.1 Tutorial: New features

{getToc} $title={Table of Contents}

Introduction to Spring Boot:


Spring Boot is an open-source Java-based framework used to create a micro service. It is developed by Pivotal Team and is used to build stand-alone and production ready spring applications.


What is Spring Boot?


Spring Boot is a project that is built on top of the Spring Framework. It provides an easier and faster way to set up, configure and run both simple & web-based applications.

It uses convention over configuration that it makes reasonable assumptions about what you need to do to get your application up and running. This feature can save you a lot of time and effort.

And it also includes a number of features that make it easier to build production-ready applications.
For example: It includes an embedded web server that you don't need to install a separate web server to run your application.


What are the benefits of using Spring Boot?

There are many benefits to using Spring Boot, including:

  • Ease of Use: It's easy to get started with Spring. You don't need to be an expert in Spring to use Spring Boot.
  • Speed: Help you to build applications faster. This is because it uses convention over configuration and includes a number of features that make it easy to build production-ready applications.
  • Production Readiness: This includes a number of features that make it easy to build production-ready applications. For example: it includes an embedded web server.

How to get started with Spring Boot?


To get started with Spring Boot, you can use the Spring Initializr. It is a web-based tool that allows you to create a new Spring Boot project. In order to use the Spring Initializr, just simply go to the Spring Initializr website and enter the following information:

  1. Project name: Name of your project.
  2. Group: Group ID of your project.
  3. Artifact: Artifact ID of your project.
  4. Dependencies: The dependencies that you want to include in your project.




Once you have entered this information then click on the "Generate Project" button. And then it will then generate a new project for you.

Once you have generated a new project, then you can open it in your IDE. The project will contain a number of files such as:

  • pom.xml: This file contains the dependencies that you selected in the Spring Initializr.
  • Application.java: It is the main class of your application. And you can use this class for running your spring boot project.
  • src/main/resources/application.properties: The application.properties file contains the configuration properties for your application.

And you can start developing your application. To run your application, just simply right-click on the Application.java file and select "Run As" > "Java Application".



New features in Spring Boot 3.1:


Spring Boot 3.1 is a major release that includes a number of new features and improvements. Some of the most notable new features are:

  • Docker Compose Support: Spring Boot 3.1 includes support for Docker Compose which makes it easier to build and deploy Spring Boot applications using Docker.
  • Spring Data JPA Improvements: It also includes a number of improvements to Spring Data JPA and including support for JSONB and improved support for schema generation.
  • Reactive Programming Support: And it includes support for reactive programming, which makes it easier to build non-blocking applications.
  • Security Improvements: Including support for OAuth 2.0 and improved support for CSRF protection.


Docker Compose Support:


Docker Compose is a tool that makes it easy to build and deploy applications using Docker. You can define a set of Docker images and their relationships in a YAML file. Spring Boot 3.1 now includes support for Docker Compose to build and deploy Spring Boot applications using Docker.

First, you need to create a Dockerfile. It is a text file that defines the steps that Docker needs to take to build your image. The following is an example of a Dockerfile for a Spring Boot application:

FROM openjdk:11-jdk

RUN mkdir -p /app

WORKDIR /app

COPY target/my-app.jar /app

Once you have created a Dockerfile, you can build your image using the following command:

CMD java -jar my-app.jar

After an image has been built, you can create a Docker Compose file. The Docker Compose file is a YAML file that defines the services that you want to run. The following is an example of a Docker Compose file for a Spring Boot application:

version: '3.7'

services:
  my-app:
    image: my-app
    ports:
      - 8080:8080

Now, you can start your application using the following command:

docker-compose up


Spring Data JPA Improvements:

Spring Data JPA is a framework that makes it easy to access data from a relational database using Spring Boot. It supports for JSONB and improved support for schema generation.

JSONB is a new data type that is supported in PostgreSQL 9.4 and later. It is a binary JSON data type that can be used to store JSON data in a relational database. And Spring Boot 3.1 now includes support for JSONB that easier to store JSON data in a relational database using Spring Data JPA. For using it, you first need to add the spring-boot-starter-data-jpa dependency to your project.

Schema generation is the process of automatically generating the schema for a database table from the annotations in your Java code. Now it includes improved support for schema generation to generate the schema for a database table from the annotations in your Java code.


Reactive Programming Support:


Reactive programming is a programming paradigm that is based on the idea of asynchronous data streams. It is becoming increasingly popular as it can be used to build non-blocking applications that are more responsive to user input. Now it includes support for reactive programming and easier to build non-blocking applications using Spring Boot.

To use reactive programming with Spring Boot, you first need to add the spring-boot-starter-webflux dependency to your project. The spring-boot-starter-webflux dependency provides the necessary classes and annotations to support reactive programming.

After you have added the spring-boot-starter-webflux dependency to your project, then you can start using reactive programming in your code. The following is an example of a reactive programming controller:

@RestController
public class HelloController {

    @GetMapping("/hello")
    public Mono<String> hello() {
        return Mono.just("Hello, world!");
    }

}

The @GetMapping("/hello") annotation tells Spring Boot to map the /hello URL.


Security Improvements:


Spring Boot 3.1 includes a number of security improvements including support for OAuth 2.0 and improved support for CSRF protection.

OAuth 2.0 is a framework that allows users to grant third-party applications access to their data without having to share their passwords. And it includes support for OAuth 2.0 which makes it easier to build applications that use it.

To use OAuth 2.0 with Spring Boot, you first need to add the spring-boot-starter-oauth2-client dependency to your project. Once you have added this dependency, you can start using it to allow users to grant third-party applications access to their data.

CSRF (Cross-site request forgery) is a security vulnerability that can be used to trick users into performing actions that they did not intend to perform. It now includes improved support for CSRF protection and it more difficult for attackers to exploit it.

To protect from CSRF attacks, you first need to add the spring-boot-starter-security dependency to your project. Then you can start using CSRF protection to protect your applications from CSRF attacks.


Creating a Simple Spring Boot Spplication:


As you have already known that Spring Boot is a framework that makes it easy to create stand-alone and production-grade Spring applications. It provides a number of features that make it easy to get started with Spring such as:
  • Autoconfiguration: Spring Boot automatically configures many of the Spring features that you would typically need to configure manually.
  • Starters: It provides the starters that make it easy to add common Spring features to your application.
  • Embedded Web Server: The embedded web server that you don't need to install a separate web server to run your application.

Creating a Maven Project:


The first step in creating a Spring Boot application is to create a Maven project. You can do this using the following command:

mvn archetype:generate -DgroupId=com.example -DartifactId=my-app 
-DarchetypeArtifactId=maven-archetype-quickstart

This will create a new Maven project with the following directory structure:

my-app
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── App.java
    │   └── resources
    └── test
        ├── java
        │   └── com
        │       └── example
        │           └── AppTest.java
        └── resources


Adding Spring Boot Dependencies:


The next step is to add the Spring Boot dependencies to your project. You can do this by editing the pom.xml file and adding the following dependencies:

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

This will add the Spring Boot web starter to your project. This dependency provides a number of features that make it easy to create a web application such as:
  • Autoconfiguration: It automatically configures a number of the Spring MVC features that you would typically need to configure manually.
  • Starter: The Spring Boot web starter includes a number of other starters that make it easy to add common Spring features to your application such as Spring Data JPA and Spring Security.

Writing the Application Code:


The next step is to write the application code. The simplest Spring Boot application is a web application that returns a simple message. You can write this application by creating a new class called App.java in src/main/java/com/example package. And this class should contain the following code:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

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

}

@SpringBootApplication annotation tells Spring Boot to autoconfigure the application.


Running the Application:


The final step is to run the application. You can do this by running the following command:

mvn spring-boot:run

This will start the application and it will be available at http://localhost:8080.


Testing a Spring Boot Application:


Testing is an essential part of the development process. It helps to ensure that your application is working as expected and that it is free of bugs. There are three main types of testing: unit testing, integration testing and system testing.


Unit Testing:


Unit testing is the most basic type of testing. It involves testing individual units of code such as classes, methods and functions. And those are typically written by the developer who wrote the code being tested.

In Spring Boot, there are tools and libraries that make it easy to write unit tests. For example: The Spring Boot Test framework provides annotations that can be used to define unit tests.

Here is an example of a unit test for a Spring Boot application:


@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleControllerTest {

    @Autowired
    private ExampleController controller;

    @Test
    public void testHello() {
        String greeting = controller.hello();
        assertEquals("Hello, world!", greeting);
    }

}

This test uses @RunWith annotation to specify that the test should be run using SpringRunner class. And @SpringBootTest annotation tells Spring Boot to create a Spring Boot application context for the test. The @Autowired annotation tells Spring Boot to inject ExampleController bean into the test. And for @Test annotation tells JUnit to run the test.


Integration Testing


Integration testing involves testing how different units of code interact with each other. Those are typically written by developers and testers.


Here is an example of an integration test:


@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleServiceIntegrationTest {

    @Autowired
    private ExampleService service;

    @Test
    public void testHello() {
        String greeting = service.hello();
        assertEquals("Hello, world!", greeting);
    }

}

This test is similar to the unit test, but it uses @SpringBootTest annotation to create a Spring Boot application context that includes all of the beans in the application. This allows the test to test how the different units of code interact with each other.


System Testing


System testing is the most comprehensive type of testing. It involves testing the entire application as a whole. These kinds of test are typically written by testers.

Here is an example of a system test:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleApplicationSystemTest {

    @Autowired
    private ExampleApplication application;

    @Test
    public void testApplication() {
        application.run();
        assertEquals(200, application.getPort());
    }

}

This test uses @RunWith annotation to specify that the test should be run using the SpringRunner class. And use @SpringBootTest annotation tells Spring Boot to create a Spring Boot application context for the test. And then use @Autowired annotation tells Spring Boot to inject the ExampleApplication bean into the test. For @Test annotation tells JUnit to run the test.

The test starts the application and then checks the application's port number. And this expects the application to be running on port 8080.


Deploying a Spring Boot Application


When you have developed and tested your Spring Boot application, you need to deploy it to production. There are the ways to deploy a Spring Boot application such as:
  • To a physical server: You can deploy your Spring Boot application to a physical server by using a deployment tool such as Jenkins or Ansible.
  • To a cloud environment: Deploy Spring Boot application to a cloud environment such as Amazon Web Services (AWS), Microsoft Azure or Google Cloud Platform.

Deploying to a physical server


To deploy your application to a physical server, you will need to:
  1. Create a deployment package for your application.
  2. Upload the deployment package to the physical server.
  3. Configure the physical server to run your application.

Creating a deployment package


One of the other ways is to use the Maven build tool. To do this, you will need to add the following dependency to your project's pom.xml file:

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

And after you have added this dependency, you can create a deployment package by running the following command:

mvn clean install

This will create a JAR file in the target package of your project. JAR file is the deployment package for your application.


Uploading the deployment package to the physical server


When you have created the deployment package, you need to upload it to the physical server. And do this using a variety of methods such as FTP, SFTP or SCP.


Configuring the physical server to run your application


In order to upload the deployment package to the physical server, you need to configure the server to run your application. 

You will need to:

  1. Create a new user account for your application.
  2. Set the permissions on the deployment package so that the new user account can access it.
  3. Edit the server's startup scripts to start your application when the server boots up.

Deploying to a cloud environment


To deploy Spring Boot application to a cloud environment, you will need to:
  1. Create an account with the cloud provider.
  2. Create a new instance in the cloud provider.
  3. Configure the instance to run your application.

Creating an account with the cloud provider


To create an account with a cloud provider, you will need to visit the provider's website and fill out the registration form.


Creating a new instance in the cloud provider


Once you have created an account with a cloud provider, you can create a new instance. To do this, you will need to:
  1. Select the type of instance that you want to create.
  2. The region and availability zone where you want to create the instance.
  3. Amount of storage that you want to allocate to the instance.
  4. Operating system that you want to install on the instance.
  5. And then click the "Create" button.

Configuring the instance to run your application


After you have created a new instance, you need to configure the instance to run your application. 
  1. Install the Java Runtime Environment (JRE) on the instance.
  2. Upload the deployment package to the instance.
  3. Edit the instance's startup scripts to start your application when the instance boots up.


Conclusion

Spring Boot is a powerful framework that can help you to build Java applications faster and easier. If you are new to Spring, I highly recommend using Spring Boot. It will make your life much easier.

This tutorial has shown you how to create a simple Spring Boot application. Spring Boot makes it easy to create stand-alone and production-grade Spring applications.

And testing is an essential part of the development process. It helps to ensure that your application is working as expected and that it is free of bugs. There are three main types of testing: unit testing, integration testing and system testing.

Lastly, deploying a Spring Boot application is a straightforward process. There are a number of ways to deploy a Spring Boot application and the best way for you will depend on your specific needs.

Previous Post Next Post