當前位置:
首頁 > 最新 > Spring中抽象類中能不能進行@autoware?

Spring中抽象類中能不能進行@autoware?

之前做項目中,有時候會突發奇想,這個就是在菜鳥階段想到的,所以對java思想理解的比較低,勿噴。

當時還真發現一些有意思的東西,我們可以思考下,

(1)stackoverflow中的一個答案,說的是,抽象類不能被實例化,需要進行繼承實現;當你去繼承之後,實現了抽象方法時,此時會創建一個匿名類,不是父類的實例化;有多個子類實現時,進行autoware注入,spring不知道which bean去注入;

(2)Abstract classes can』t be instantiated, you need to use a concrete implementation. Same as in regular java, if you try to instantiate an abstract class, it tells you to implement the abstract methods within. When you do, an anonymous class is created. It』s not an instatiation of the abstract class, but a new subclass of that abstract class.

Spring will look for classes which extend your base class, being Report1 and Report2, Spring sees it has multiple classes which match the requirements and doesn』t know which one to choose. thus you get the error that there are multiple matching beans.

You can fix this by making an 「adapter」 basicly create a concrete class which extends your base-class, implements the abstract methods, but doesn』t do anything them. Then you can autowire that implementation and test against it. However your abstract class should allready be tested due to the fact you are testing report 1 and 2. If errors still occur with your base class, it means logic you don』t use is causing bugs, which is a bad practice anyway. also with a test coveage tool you could spot unused code that way.

(3)在抽象類中需要注入(@autowire),在繼承了該抽象類的子類中同樣需要再次注入。比如在項目中用戶購買物品的模板抽象類中需要使用userservice,則在改抽象類中和道具服務類(實現了道具介面並繼承了模板抽象類)均需要對userservice進行注入。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 推酷 的精彩文章:

命令行的藝術:linux bash命令大全詳解

TAG:推酷 |

您可能感興趣

Fate/stay night,沒有金閃閃C媽必勝?saber不能與之匹敵!
mastermind JAPAN x Suicoke的暗黑拖鞋,能不能讓你再次主動掏錢包?
為什麼僱主不能支付你在Cryptocurrency
黑到不能再黑!Nike Flyknit Trainer 「Triple Black」 即將發售
《Sugar Man》少時Sunny吐槽Henry:總是問跟我結婚能不能拿SM股份
Yann LeCun卸任!Facebook變天,做AI不能落地是不成了
李冰冰和Giorgio Armani之間不能說的秘密
完整的Rolls-Royce不能沒有「—」
誰說摩托就不能性價比?Modenas Pulsar NS200 震撼市場!
你最不能錯過的就是 GOAT x James Harden 的全明星大爬梯
【視頻鑒賞】美背撩人,這個感覺不能太爽——2017 GrandSlam Hong Kong
不能被新iPhone搶風頭 iPhone X或再推新配色?
Springwood春樹村一個你不能錯過的花坊
World Duty Free出正式道歉聲明了,但我們不能接受
索尼Xperia XZ2和XZ2 Compact正式發布,這些背後消息你不能不知
強化AR應用的ARCore 1.0 並不能替Google建立優勢
誰說Win平台Pr用戶不能輸出Prores編碼來著?
iPhone通話bug發酵:之前不能接 現在掛不了
漫威迷不能錯過的Black Panther x BAIT x PUMA三方聯名 PUMA Basket詮釋70年代的街頭氣息
iPhone X再曝問題:不能接電話!看看iPhone X問題匯總