What Are the New Features of SpringBoot3 ?
Omar Ismail
Senior Software Engineer @ Digitinary | Java 8 Certified? | Spring & Spring Boot?????? | AWS? | Microservices ?? | RESTFul Apis & Integrations ?? FinTech ?? | Open Banking ?? | Digital Payments and Transformation??
Thanks to : https://medium.com/javarevisited/what-are-the-new-features-of-springboot3-6ddba9af664
1. Java17
The first and most important thing we are more concerned about is that the Spring Boot3 version supports Java17 at least, it is time to upgrade. Here are some important changes after Java 17.
1.1 Record
The record was introduced in Java14.
In the past, we needed to write a bunch of gets and set methods to write a class. Later?Lombok, these methods were saved. Now Java provides us with native writing methods.
public record User() {}
In essence, the class modified by record is final, and its parent class is not Object, but?java.lang.Record.
Record class properties can only be declared in the header, all member variables are public final, and only static properties can be declared, but member methods and static methods can be declared.
public record User(String username) {
static int id;
public String getName(){
return this.username;
}
}
1.2 Text blocks
Text blocks?were introduced in Java13 and made permanent in Java15.
In the past, when we copied a multi-line string to Java, line terminators would be added automatically.
String sql = "SELECT\n" +
"\t* \n" +
"FROM\n" +
"\tsys_user0 \n" +
"WHERE\n" +
"\tuser_name = 'abc'";
The text block function can help us more conveniently define a string literal containing multiple lines of text, which uses triple quotation marks as the start and end separators.
String sqlBlock = """
SELECT
*
FROM
sys_user0
WHERE
user_name = 'abc'
""";
1.3 Switch expression
Switch expressions were introduced in Java12 and made permanent in Java14.
The upgraded switch contains two features, one is to allow the case to use multiple constants, and the other is to have a return value.
The new?case x->?syntax is more concise in use, and there is no need to write a break for each case.
String name = "Tom";
int ret = switch (name) {
case "A" -> 1;
case "Tom", "Jack" -> 2;
default -> 0;
};
1.4 Pattern Matching Pattern Matching
Pattern matching can help us simplify our?instanceofcode.
if (obj instanceof String s) {
System.out.println(s.toLowerCase());
}
Can also be used in switch-case statements:
static double getDoubleUsingSwitch(Object o) {
return switch (o) {
case Integer i -> i.doubleValue();
case Float f -> f.doubleValue();
case String s -> Double.parseDouble(s);
default -> 0d;
};
}
1.5 Sealed sealed class
Sealed was introduced in Java15 and became a permanent feature in Java17.
The main function of the sealed class is to limit the inheritance of the class.
For example, we have the Animal class, Dog and Cat inherit it respectively, and implement the eat method. Their eating actions are different, but we don’t want people to inherit the Animal, and he is not allowed to inherit the animal’s eating behavior, just like the following This restricts it to be a sealed class through the sealed and permits keywords, and only cats and dogs can inherit it.
领英推荐
It should be noted that the subclass must be sealed, non-sealed or final after the parent class is defined as sealed.
public abstract sealed class Animal permits Cat, Dog {
public abstract void eat();
}
public non-sealed class Dog extends Animal{
@Override
public void eat() {
System.out.println("dog eat");
}
}
public non-sealed class Cat extends Animal{
@Override
public void eat() {
System.out.println("cat eat");
}
}
2. Jakarta EE 9
Another important change is that after this upgrade, it only supports Jakarta EE 9 at least, using Servlet5.0 and JPA3.0 specifications, but the latest version RC2 has been upgraded to JakartaEE 10, using Servlet6.0 and JPA3.1 specifications by default.
Some students may not even know what Jakarta is. This English word means Jakarta, the capital of Indonesia. We know that JavaEE is called JakartaEE after it is renamed. For example, our previous?javax.servletpackage is now called?jakarta.servlet.
Therefore, all imports that use objects such as HttpServletRequest in the code need to be modified.
import javax.servlet.http.HttpServletRequest;
// Change
import jakarta.servlet.http.HttpServletRequest;
3. Spring Native
Spring Native is also a major feature of the upgrade. It supports using GraalVM to compile Spring applications into locally executable image files, which can significantly improve startup speed, and peak performance, and reduce memory usage.
Our traditional applications are compiled into bytecode, then interpreted by, JVM and finally compiled into machine code to run, while Spring Native is compiled into machine code in advance through AOT, and statically compiled into executable files directly at runtime. Depends on the JVM.
4. Demo
Here I will briefly demonstrate how to use it. First, we need to do some preparatory work:
Then create a new project through Spring Initialzr, use the latest version of Spring Boot 3.0.0-SNAPSHOT, check?GraalVM Native Support, and add a test after creating the project?Controller.
@RestController
public class TestController {
@GetMapping("/")
public String hello(){
return "GraalVM ...";
}
}
Then run the program directly and find that the startup time takes about 1 second.
Then execute the command to generate the image file:
./gradlew nativeCompile
This process is quite time-consuming, and it took about 2 minutes to generate it.
Finally, execute the command:
./build/native/nativeCompile/demo2
We see that the final startup time is 0.082 seconds, more than 10 times faster.
Here I use Gradle, if you use maven, use the following command:
1. mvnw -Pnative native:compile
2. ./target/demo2
5. Other dependency upgrades
Spring Boot 3 depends on the Spring6 version at least, so the corresponding Spring version should also be changed (no one is still using Spring2), and other dependencies are upgraded as follows:
? Kotlin 1.7+
? Lombok 1.18.22+ (JDK17 support version)
? Gradle 7.3+
In addition, what I want to say is that SpringBoot2.7 introduces a new automatic assembly method?META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. The original writing method?spring.factoriesis still compatible below version 3.0. After the new version 3.0, the old writing method?spring.factoriescannot be used. Middleware-related development students should pay attention.
Some other configuration changes and some small changes in Spring MVC will not be mentioned, and the update log can be seen at that time.
Finally, if you want to upgrade, after the new version is released, there will be a migration guide based on Spring Boot 2.7 version.
Software engineer?????? @Rays microfinance | Java | Spring boot ??| DevOps | MERN stack | React native??
1 年Thanks Omar Ismail