irpas技术客

Spring-boot 2.6.x新特性_java_nn

网络 1375

一、引子

自Spring-boot 2.6.0发以来,还没来得及深入研究下其新特性,直到朋友问起关于Spring循环依赖时正好使用了最新版本的Spring-boot,讲解过程中编写的示例代码运行出错:

┌─────┐ | a (field private com.demo.TestB com.demo.TestA.b) ↑ ↓ | b (field private com.demo.TestA com.demo.TestB.a) └─────┘ Action: Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true. 二、Spring-boot 2.6新特性

下面列举几个自认觉得需要注意的新特性,详细的新特性参见spring官网呈现:Spring boot 2.6 新特性。

1、默认禁止了循环依赖

关于Spring的循环依赖问题,也是面试中频繁遇到的问题之一。通常情况下一个合理的设计是完全可以避免对象的循环依赖的,但是也不能排除会有那么些奇葩的情况会出现循环依赖,Spring Boot也提供了折中解决办法,在报错信息中已经明示:

As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

需要我们在配置文件application.yml中配置allow-circular-references为true,程序依然可以正常启动:

spring: main: allow-circular-references: true 2、嵌入了MongoDB

若要使用嵌入式 MongoDB,现在必须设置spring.mongodb.embedded.version属性(即指定使用的MongoDB版本号),确保嵌入式支持使用的 MongoDB 版本与应用程序将在生产中使用的 MongoDB 版本相匹配。

spring: mongodb.embedded.version: 3.12.7 3、网络资源配置

直接注入不再有效,改为Resources、WebProperties、WebProperties注入。

4、删除了几个依赖关系 JBoss Transaction SPI 如果项目需要,需要自行引入依赖:org.jboss:jboss-transaction-spi、org.jboss:jboss-transaction-spi。Nimbus DS 如果项目需要,需要自行引入依赖:com.nimbusds:oauth2-oidc-sdki、com.nimbusds:nimbus-jose-jwt。HAL Browser 如果项目需要,需要自行引入依赖:org.webjars:hal-browser、org.webjars:hal-browser。 5、默认使用全新匹配策略

请求路径与 Spring MVC 处理映射匹配的默认策略已从AntPathMatcher更改为PathPatternParser。你可以设置spring.mvc.pathmatch.matching-strategy为ant-path-matcher来改变它。

2.6.0之前: public static class Pathmatch { private MatchingStrategy matchingStrategy = MatchingStrategy.ANT_PATH_MATCHER; } 2.6.0之后: public static class Pathmatch { private MatchingStrategy matchingStrategy = MatchingStrategy.PATH_PATTERN_PARSER; }

两者差异上:PathPattern去掉了ANT字样,但保持了很好的向下兼容性:除了不支持将 ** 写在path中间之外,其它的匹配规则从行为上均保持和AntPathMatcher一致,并且还新增了强大的 {*pathVariable} 的支持。

6、端点现在可以在密钥下公开 Java 运行时信息

如下:

{ "java": { "vendor": "Zulu", "version": "17", "runtime": { "name": "OpenJDK Runtime Environment", "version": "25.292-b10" }, "jvm": { "name": "OpenJDK 64-Bit Server VM", "vendor": "Zulu", "version": "25.292-b10" } } } 7、其他变化 Servlet应用现在支持在Cookie中添加SameSite。支持在主端口或管理端口上配置健康组。在 Spring Boot 2.4 中弃用的类、方法和属性已在此版本中删除。支持 Log4j2 复合配置支持构建信息属性排除

另外需要注意的是,Spring Boot每年会在5月份和11月份发布两个中型版本,每个中型版本提供1年的免费支持,也就意味着2.4.x已经停止了版本停止(免费)支持。不过对本次版本更新点有所了解即可。


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #springboot #26x新特性 #自Springboot