Spring 6.0 將停止支持 Freemarker 和 JSP
Spring Framework 6.0 第一個里程碑版本已經(jīng)發(fā)布,目前已經(jīng)可以從Spring Repo獲取。這里有一些新變更我們可以提前了解一下。
Java EE遷移
甲骨文已經(jīng)把Java EE捐獻(xiàn)給Eclipse基金會數(shù)年了。Java EE的名稱也變更為了Jarkarta EE,包名也相應(yīng)地從javax變更為jakarta。例如javax.persistence現(xiàn)在對應(yīng)為jakarta.persistence。
核心容器
在本次里程碑版本中涉及到的兩個核心容器規(guī)范JSR-250和JSR-330的包名都會遷移到Jakarta EE。
持久層
Jakarta EE的持久層規(guī)范也將在此次里程碑版本中完成遷移。這意味著javax.persistence和jakarta.validation都將實(shí)裝。對應(yīng) Hibernate ORM 5.6.x 和 Hibernate Validator 7.0.x 。
Web 應(yīng)用
Servlet中間件基準(zhǔn)線
由于Jakarta EE的合并遷移,Servlet中間件也要進(jìn)行升級。Tomcat 10, Jetty 11, 或者基于undertow-servlet-jakarta 的 Undertow 2.2.14 是目前里程碑版本的基準(zhǔn)線。
進(jìn)一步移除過時API
一些過時的基于Servlet的組件已經(jīng)在本次里程碑版本中移除。
Commons FileUpload 上傳組件已經(jīng)被移除。
相關(guān)的前后端模板Tiles布局組件例如FreeMarker、JSP停止了支持?,F(xiàn)在Spring將精力放在了基于Restful的Web架構(gòu)。
Controller掃描機(jī)制變動
現(xiàn)在Spring MVC和Spring WebFlux將不再將類上單獨(dú)有@RequestMapping的Spring Bean視為控制器。在6.0之前默認(rèn)情況以下代碼是可以的:
- /**
- * 6.0之前
- * @author felord.cn
- */
- @Component
- @RequestMapping("/foo")
- public class FooController {
- @GetMapping("/hello")
- public Map<String, String> hello() {
- return Collections.singletonMap("hello", "world");
- }
- }
6.0之前相關(guān)基于AOP的代理機(jī)制將失效, 請為此類控制器啟用基于類的代理 。
在6.0之后默認(rèn)情況下必須有@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<String, HttpMethod> 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<HttpMethod>, Serializable {
- private static final long serialVersionUID = -70133475680645360L;
- private static final HttpMethod[] values;
- private static final Map<String, HttpMethod> 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的第二個里程碑和對應(yīng)的Spring Boot 3.0第一個里程碑將和大家見面。