痛快!SpringBoot终于禁掉了循环依赖!
管她前浪,还是后浪?
能浪的浪,才是好浪!
每天 10:33 更新文章,每天掉亿点点头发...
源码精品专栏
Spring的Bean管理,一直是整个体系中津津乐道的东西。尤其是Bean的循环依赖,更是很多面试官最喜欢考察的2B知识点之一。
但事实上,项目中存在Bean的循环依赖,是代码质量低下的表现。多数人寄希望于框架层来给擦屁股,造成了整个代码的设计越来越糟,最后用一些奇技淫巧来填补犯下的错误。
还好,SpringBoot终于受不了这种滥用,默认把循环依赖给禁用了!
从2.6版本开始,如果你的项目里还存在循环依赖,SpringBoot将拒绝启动!
验证代码小片段
为了验证这个功能,我们只需要两段小代码。
CircularDependencyA.java
@Component
@RequiredArgsConstructor
publicclassCircularDependencyA{
privatefinal CircularDependencyB circB;
}
CircularDependencyB.java
@Component
@RequiredArgsConstructor
publicclassCircularDependencyB{
privatefinal CircularDependencyA circA;
}
RequiredArgsConstructor注解,是lombok包里面的,用来实现简单的构造器注入。不出所料,当我们启动代码的时候,报错了~~
报错如下:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| circularDependencyA defined in file [cir/CircularDependencyA.class]
↑ ↓
| circularDependencyBdefinedinfile [cir/CircularDependencyB.class]
└─────┘
Action:
Relyinguponcircularreferencesisdiscouragedandtheyareprohibitedbydefault. Updateyourapplicationtoremovethedependencycyclebetweenbeans. Asalastresort, itmaybepossibletobreakthecycleautomaticallybysettingspring.main.allow-circular-referencestotrue.
当然,有些鸟人已经玩大了,循环依赖到处都是,改代码变的越来越不现实。那你还可以通过在yaml里配置参数来临时开启循环依赖。
spring.main.allow-circular-references=true
看来SpringBoot对恶势力的容忍能力还是不够坚决啊!
绕过SpringBoot这个拦截的方法不止一种,比如使用@Lazy注解进行延迟初始化。但这些都是治标不治本,辜负了SpringBoot的一片苦心。
基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能。项目地址:https://github.com/YunaiV/ruoyi-vue-pro
做对的事
其实,我们一直把代码往下找下去,会发现这个开关,其实是Spring的功能。
AbstractAutowireCapableBeanFactory#allowCircularReferences
/** Whether to automatically try to resolve circular references between beans. */
privateboolean allowCircularReferences = true;
很长一段时间,SpringBoot这个值都是默认为true的。但这种纵容造成了大批低质量的代码产生,以至于新员工一直在给老员工擦屁股。
把这个值默认设置为false,是坚持做对的事情。起码,在工程师编写出质量不高的代码时,能够知道他自己在做什么,而不是把隐患一步步的推迟,任代码腐败。
不得不为SpringBoot点个赞。真棒!
欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,
长按下方二维码噢:
已在知识星球更新源码解析如下:
最近更新《芋道 SpringBoot 2.X 入门》系列,已经 101 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。
提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。
文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。