最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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該用哪個的評論 (共 條)

分享到微博請遵守國家法律
鱼台县| 通化市| 阿拉善左旗| 北安市| 苏州市| 伊宁市| 石阡县| 柞水县| 通江县| 忻城县| 林周县| 婺源县| 莫力| 贵州省| 化州市| 磴口县| 万全县| 兴安县| 浦县| 淮安市| 新民市| 东海县| 奎屯市| 刚察县| 衡阳县| 当雄县| 施甸县| 德安县| 渑池县| 南靖县| 沙湾县| 雷波县| 赤壁市| 潼南县| 望奎县| 龙泉市| 南木林县| 鹤峰县| 九龙坡区| 东兴市| 宜宾市|