簡體   English   中英

OSGI-選擇要從另一個Component類激活的捆綁軟件

[英]OSGI - Choose which bundle to activate from another Component class

我想開發OSGI存儲服務,所以我創建了一個名為Store的接口。 現在,我有兩個實現此接口的類。

SqlStorage.java

@Component(immediate = true)
@Service
public class SqlStorage implements Store {
    //some code here
}

MongoStorage.java

@Component(immediate = true)
@Service
public class MongoStorage implements Store {
    //some code here
}

現在,我還有另一個依賴於Store捆綁包。

@Component(immediate = true)
@Service
public class StoreManager implements StoreService {
    // how can I select one of the implmentation here
    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    public Store store;
}

StoreManager如何選擇要使用的Store實現?

假設StoreManager能夠選擇使用SQL還是MongoDB作為存儲。

這種情況可行嗎?

請問你的店經理決定使用哪種存儲的實現? 正如您當前對它們的定義一樣,Store實現是等效的且同樣是不錯的答案,因此OSGi將任意選擇一個。

如果這不是您想要的,則需要添加服務屬性以區分商店。 例如:

@Component(property = { "region=NorthAmerica", "language=en_US" })
@Component(property = { "region=Europe", "language=en_GB" })
@Component(property = { "region=Asia", "language=jp" })

然后,您的商店經理可以使用目標過濾器選擇所需的商店。 例如,如果它想要一家英語商店(但不關心美國英語和英國英語),則可以使用以下過濾器:

@Reference(target = "(language=en*)")

暫無
暫無

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

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