【发布时间】:2014-10-30 14:54:43
【问题描述】:
我一直在尝试将自定义 PropertySource 添加到 spring Environment bean,但无法使其正常工作。我有一个 Spring Boot 应用程序并设法做到以下几点
@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);
}
这肯定会在StandardServletEnvironment 中添加DatabasePropertySource,但没有任何数据,因为ConfigurationDao 即@Autowired 为空。我已经在其他地方连接了ConfigurationDao,它确实有效,并且可以通过它访问数据。我只是认为这是启动期间的时间问题,但我不确定如何订购/计时。有没有人做过类似的事情并且可以提供任何帮助来实现这一点。
【问题讨论】:
标签: java spring spring-boot configuration spring-data-jpa