五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Spring中注解的@Autowired和@Resource該用哪個

2023-11-04 22:46 作者:凱小吧  | 我要投稿

Spring中注解的@Autowired和@Resource該用哪個

有一次斷點看到了一個比較奇怪的現(xiàn)象 Field injection is not recommended

注入不給推薦

環(huán)境背景 JDK 8

spring-boot-starter-parent 2.6.0

Spring 注入的三種方式 字段注入

@Autowired?? private DependA dependA;?? @Autowired?? private DependB dependB; 方法注入

private DependA dependA; private DependB dependB; ? @Autowired public void setDependA(DependA dependA) { ??this.dependA = depenA; } ? @Autowired public void setDepenB(DepenB depenB) { ??this.depenB = depenB; } 構(gòu)造器注入

private final DependA depenA; private final DependB dependB; public TestService(DependA dependA, DependB dependB) { ??this.dependA = dependA; ??this.dependB = dependB; } @Autowired VS @Resource @Autowired 是Spring定義的,而 @Resource 是JSR-250定義。

依賴識別方式:@Autowired默認是byType,可以使用@Qualifier指定Name,@Resource默認使用ByName,如果找不到則使用ByType。

適用對象:@Autowired可以對構(gòu)造器、方法、字段使用,@Resource只能對方法、字段使用。

@Resource 為什么沒有不推薦 其實這是因為 @Autowired 是 Spring 提供的特定注解,和 Spring 框架綁定,而 @Resource 是JSR-250提供的,它是Java標準,作為 IOC 容器框架都是需要實現(xiàn)的,所以不存在上面的第一點問題。 一些最佳實踐 Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies. Note that use of the @Required annotation on a setter method can be used to make the property a required depen. 翻譯:由于可以混合使用基于構(gòu)造函數(shù)和基于setter的DI,因此使用構(gòu)造函數(shù)作為強制依賴項,使用setter方法或配置方法作為可選依賴項是一條很好的經(jīng)驗法則。請注意,在setter方法上使用@Required注釋可以使屬性成為必需的依賴項。 public class TestService { ??//必須依賴 ??private final DependA dependA; ??//非必須依賴 ??private DependB dependB; ? ??public TestService(DepenA depenA) { ????this.dependA = dependA; ?} ? ??@Autowired(required = false) ??public void setDependB(DependB dependB) { ????this.dependB = dependB; ?} } 使用構(gòu)造器注入帶來的循環(huán)依賴問題 如果我們使用構(gòu)造器注入,那么可能會出現(xiàn)無法解析循環(huán)依賴的問題。這里提供兩個解決方案: 使用方法注入(官網(wǎng)推薦) One possible solution is to edit the source code of some classes to be configured by setters rather than constructors. Alternatively, avoid constructor injection and use setter injection only. In other words, although it is not recommended, you can configure circular dependencies with setter injection. 翻譯:一種可能的解決方案是編輯一些類的源代碼,由setter而不是構(gòu)造函數(shù)進行配置?;蛘?,避免構(gòu)造函數(shù)注入,只使用setter注入。換句話說,盡管不建議這樣做,但您可以使用setter注入來配置循環(huán)依賴關(guān)系。 小編沒用過@Servicepublic+@Lazy @Servicepublic class DepenA { ??private final TestService testService; ??public DependA(@Lazy TestService testService) { ????this.testService = testService; ?} } @Servicepublic class TestService { ??private final DependA dependA; ? ??public TestService(DependA dependA) { ????this.dependA = dependA; ?} }

Spring中注解的@Autowired和@Resource該用哪個的評論 (共 條)

分享到微博請遵守國家法律
巨野县| 太原市| 沐川县| 清水河县| 库车县| 江陵县| 穆棱市| 抚州市| 乌恰县| 金阳县| 辽阳市| 丽江市| 石嘴山市| 云浮市| 承德市| 旅游| 石嘴山市| 仙游县| 海安县| 土默特右旗| 铜鼓县| 辽阳县| 海安县| 五河县| 喀喇沁旗| 建宁县| 三河市| 义马市| 海伦市| 泰来县| 屏边| 凤台县| 花莲县| 宁海县| 大厂| 伊宁县| 隆尧县| 吴江市| 墨竹工卡县| 杭锦旗| 五常市|