前瞻Spring6。0将停止支持Freemarker和JSP
Spring Framework 6.0 第一个里程碑版本已经发布,目前已经可以从 Spring Repo 获取。这里有一些新变更我们可以提前了解一下。 Java EE迁移
甲骨文已经把 Java EE 捐献给 Eclipse 基金会数年了。 Java EE 的名称也变更为了 Jarkarta EE ,包名也相应地从 javax 变更为jakarta 。例如javax.persistence 现在对应为jakarta.persistence 。 核心容器
在本次里程碑版本中涉及到的两个核心容器规范 JSR-250 和 JSR-330 的包名都会迁移到 Jakarta EE 。 持久层
Jakarta EE 的持久层规范也将在此次里程碑版本中完成迁移。这意味着 javax.persistence 和jakarta.validation 都将实装。对应 Hibernate ORM 5.6.x 和 Hibernate Validator 7.0.x 。 Web 应用Servlet中间件基准线
由于 Jakarta EE 的合并迁移, Servlet 中间件也要进行升级。 Tomcat 10 , Jetty 11 , 或者基于 undertow-servlet-jakarta 的 Undertow 2.2.14 是目前里程碑版本的基准线。 进一步移除过时API
一些过时的基于 Servlet 的组件已经在本次里程碑版本中移除。 Commons FileUpload 上传组件已经被移除。 相关的前后端模板 Tiles 布局组件例如 FreeMarker 、 JSP 停止了支持。现在Spring将精力放在了基于 Restful 的 Web 架构。 Controller扫描机制变动
现在 Spring MVC 和 Spring WebFlux 将不再将类上单独有 @RequestMapping 的Spring Bean视为控制器。在6.0之前默认情况以下代码是可以的: /** * 6.0之前 * @author felord.cn */ @Component @RequestMapping("/foo") public class FooController { @GetMapping("/hello") public Map hello() { return Collections.singletonMap("hello", "world"); } }
6.0 之前相关基于 AOP 的代理机制将失效, 请为此类控制器启用基于类的代理 。
在 6.0 之后默认情况下必须有 @Controller 或 @RestController 注解才可以被视为控制器。 HttpMethod
请求方法 HttpMethod 在6.0之前为Java枚举。 /** * 6.0 之前 * * @since 3.0 */ public enum HttpMethod { GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE; private static final Map mappings = new HashMap<>(16); static { for (HttpMethod httpMethod : values()) { mappings.put(httpMethod.name(), httpMethod); } } @Nullable public static HttpMethod resolve(@Nullable String method) { return (method != null ? mappings.get(method) : null); } public boolean matches(String method) { return name().equals(method); } }
在 6.0 以后改为Java类: public final class HttpMethod implements Comparable, Serializable { private static final long serialVersionUID = -70133475680645360L; private static final HttpMethod[] values; private static final Map mappings = new HashMap<>(16); public static final HttpMethod GET = new HttpMethod("GET"); public static final HttpMethod HEAD = new HttpMethod("HEAD"); public static final HttpMethod POST = new HttpMethod("POST"); public static final HttpMethod PUT = new HttpMethod("PUT"); // 其它省略 } 其它前沿
在2022年的1月份 Spring Framework 6.0 的第二个里程碑和对应的Spring Boot 3.0第一个里程碑将和大家见面。