[英]How to enable @ConditionalOnProperty in spring-boot on runtime changeable property
我有兩個類依賴於config變量:
@Component
@ConditionalOnProperty("config.db")
public class DatabaseTokenStore implements TokenStore {
}
@Component
@ConditionalOnMissingBean(DatabaseTokenStore.class)
public class SimpleTokenStore implements TokenStore {
}
因此當db為true時, DatabaseTokenStore
類在false時自動裝配,然后SimpleTokenStore
自動裝配。 問題是我可以在運行時使用CRaSH
更改此屬性。 然后這個機制將無法工作。 有沒有辦法在運行時更改界面的實現?
在啟動時初始化兩個TokenStore
。 並創建一個解析器以注入您需要使用它們的類。 像這樣:
@Component
public class HelloStoreResolver {
@Autowired
private HelloStore oneHelloStore;
@Autowired
private HelloStore twoHelloStore;
public HelloStore get() {
if (condition) {
return oneHelloStore;
} else {
return twoHelloStore;
}
}
}
@Component
public class HelloController {
@Autowired
private HelloStoreResolver helloResolver;
//annotations omitted
public String sayHello() {
return helloResolver.get().hello();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.