簡體   English   中英

如何通過提供接口名稱來獲取實現的bean

[英]How to get bean of an implementation by giving the interface name

在Google Guice中,我們可以定義如下接口和實現:

@ImplementedBy(MyImplementation.class)
interface MyInterface {
    ....
}

class MyImplementation implements MyInterface {

}

如果我們使用:

injector.getInstance(MyInterface.class);

MyImplementation的實例返回,因為它知道MyImplementation實現了MyInterface。

Spring中有沒有類似的方法可以實現這一目標?

非常感謝。

在Spring中,您可以使用org.springframework.stereotype包中的注釋之一(例如@Service)來注釋實現。

如果然后使用類路徑掃描來選擇實現,則可以從Bean存儲中獲取接口的實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM