0
我一直試圖向spring Environment
bean添加自定義PropertySource
,但無法使其工作。我有一個春天啓動的應用程序,並已成功地做到以下幾點如何在Spring引導中將基於JPA的PropertySource添加到外部配置
@Bean
public Environment environment() {
ConfigurableEnvironment environment = new StandardServletEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addFirst(new DatabasePropertySource("databaseProperties"));
return environment;
}
public class DatabasePropertySource extends PropertySource<DatabaseReaderDelegate> {
public DatabasePropertySource(String name) {
super(name, new DatabaseReaderDelegate());
}
@Override
public Object getProperty(String name) {
return this.source.getProperty(name);
}
}
public class DatabaseReaderDelegate {
@Autowired ConfigurationDao dao;
public Object getProperty(String property) {
Configuration object = dao.findOneByConfKey(property);
Object value = object.getConfValue();
return value;
}
}
public interface ConfigurationDao extends JpaRepository<Configuration, Long> {
Configuration findOneByConfKey(String name);
}
這無疑增加了DatabasePropertySource
到StandardServletEnvironment
但有ISN」 t的數據爲ConfigurationDao
即@Autowired
爲空。我在其他地方連接了ConfigurationDao
,它確實可以工作,並且可以通過它訪問數據。我只是認爲這是啓動期間的時間問題,但我不確定如何訂購/定時。有沒有人做過類似的事情,並有任何幫助可以做到這一點。
嘆息,所有這些工作只是爲了從數據庫進行配置。這讓懶惰的開發人員陷入了困境:( – Gareth 2014-09-08 11:24:29
懶惰是好的,它是一個鍋爐板模式,所以也許它應該在Spring Boot中(在github中引發問題很容易,並且幾乎和發送pull請求一樣容易)。 – 2014-09-09 10:51:31