1
我們有一個應該在運行時確定依賴關係的場景。一個例子說明如下細節:如何在春季實現動態依賴注入?
public class OrderProcessor {
// The Validator should be determinded based on the version of the service.
private Validator orderProcessValidator;
public Confirmation process(Order order) {
if(orderProcessValidator.validate(order)) {
// Business logic
}
}
}
可以使用Spring IOC動態注入Validator
,或只能通過工廠模式來解決?
確定使用哪個驗證程序的邏輯是什麼? (你說的是「服務的版本」,但是我在你的代碼中看不到'服務') –
版本我的意思是api版本控制。在這個例子中,假設我們有2個版本的「Order」。版本2.0具有更多數量的字段,並且需要一個單獨的驗證程序,而不是版本爲1.0的字段較少的驗證程序。在我們的應用程序中,我們已經有了2個驗證器,但是我們需要知道一個機制,如何使用spring在正確的場景下注入正確的驗證器。對於我們來說,保留業務邏輯是相同的,所以它被同一個OrderProcessor處理。 – Bharimalla