警告: 二进制文件CustUser包含com.flydean.records.CustUser
Compiled from "CustUser.java"
public final class com.flydean.records.CustUser extends java.lang.Record {
public com.flydean.records.CustUser(java.lang.String, java.lang.String, com.flydean.records.Address, int);
public java.lang.String toString();
public final int hashCode();
public final boolean equals(java.lang.Object);
public java.lang.String firstName();
public java.lang.String lastName();
public com.flydean.records.Address address();
public int age();
}
public final class CustUser extends java.lang.Record {
private final java.lang.String firstName;
private final java.lang.String lastName;
private final com.flydean.records.Address address;
private final int age;
public CustUser(java.lang.String firstName, java.lang.String lastName, com.flydean.records.Address address, int age) { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public java.lang.String firstName() { /* compiled code */ }
public java.lang.String lastName() { /* compiled code */ }
public com.flydean.records.Address address() { /* compiled code */ }
public int age() { /* compiled code */ }
}
@Test
public void useOldSwitch(){
switch (MONDAY) {
case MONDAY:
case FRIDAY:
case SUNDAY:
System.out.println(6);
break;
case TUESDAY:
System.out.println(7);
break;
case THURSDAY:
case SATURDAY:
System.out.println(8);
break;
case WEDNESDAY:
System.out.println(9);
break;
}
}
上面的例子中,我们想要匹配所有的星期,然后打印出相应的结果。写了很多个case语句,不美观。
再看一下新版本的例子:
@Test
public void useNewSwitch(){
switch (MONDAY) {
case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
case TUESDAY -> System.out.println(7);
case THURSDAY, SATURDAY -> System.out.println(8);
case WEDNESDAY -> System.out.println(9);
}
}
一个漂亮的连写,将一切都带走。
注意这里switch语句没有返回值,所以并不需要default语句。
考虑一个在switch中赋值的情况:
@Test
public void oldSwitchWithReturnValue(){
int numLetters;
switch (MONDAY) {
case MONDAY:
case FRIDAY:
case SUNDAY:
numLetters = 6;
break;
case TUESDAY:
numLetters = 7;
break;
case THURSDAY:
case SATURDAY:
numLetters = 8;
break;
case WEDNESDAY:
numLetters = 9;
break;
default:
throw new IllegalStateException("这天没发见人!");
}
}
传统方式我们需要定义一个局部变量,并在case中给这个局部变量赋值。
我们看下怎么使用新版的switch替换:
@Test
public void newSwitchWithReturnValue(){
int numLetters = switch (MONDAY) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
default -> throw new IllegalStateException("这天没发见人!");
};
}
package com.example.geometry;
public abstract sealed class Shape
permits com.example.polar.Circle,
com.example.quad.Rectangle,
com.example.quad.simple.Square {...}
迁移到Jakarta EE
除了下面一些spring依赖包的更新之外:
Spring Framework 6.0.
Spring AMQP 3.0.
Spring Batch 5.0.
Spring Data 2022.0.
Spring GraphQL 1.1.
Spring HATEOAS 2.0.
Spring Integration 6.0.
Spring Kafka 3.0.
Spring LDAP 3.0.
Spring REST Docs 3.0.
Spring Retry 2.0.
Spring Security 6.0
Spring Session 3.0
Spring WS 4.0.
spring boot3最大的变化就是把Java EE 迁移到了Jakarta EE,也就是说我们需要把 javax.* 替换成为 jakarta.*。
举个例子HttpServletRequest需要从:
import javax.servlet.http.HttpServletRequest;
替换成为:
import jakarta.servlet.http.HttpServletRequest;
GraalVM Native Image Support
Spring Boot3的一个非常大的功能点就是可以利用Spring的AOT技术,将spring boot的应用编译成为native的image,从而大大提升系统的运行效率。