Java 如何在Spring boot中将基于JPA的PropertySource添加到外部配置中

Java 如何在Spring boot中将基于JPA的PropertySource添加到外部配置中,java,spring,spring-boot,configuration,spring-data-jpa,Java,Spring,Spring Boot,Configuration,Spring Data Jpa,我一直在尝试向spring环境bean添加一个定制的PropertySource,但无法让它工作。我有一个Spring启动应用程序,并成功地执行了以下操作 @Bean public Environment environment() { ConfigurableEnvironment environment = new StandardServletEnvironment(); MutablePropertySources propertySources = environmen

我一直在尝试向spring
环境
bean添加一个定制的
PropertySource
,但无法让它工作。我有一个Spring启动应用程序,并成功地执行了以下操作

@Bean
public Environment environment() {
    ConfigurableEnvironment environment = new StandardServletEnvironment();
    MutablePropertySources propertySources = environment.getPropertySources();  
    propertySources.addFirst(new DatabasePropertySource("databaseProperties"));
    return environment;
}


公共接口配置DAO扩展了JPA存储{
配置findOneByConfKey(字符串名称);
}

这无疑会将
数据库属性源
添加到
标准ServleteEnvironment
中,但是没有任何数据,因为
配置DAO
@Autowired
为空。我已经在别处连接了
ConfigurationDao
,它确实可以工作,并且可以通过它访问数据。我只是认为这是启动过程中的时间问题,但我不确定具体如何订购/计时。有没有人做过类似的事情,并提供了帮助来实现这一点。

让JPA及时启动,将其纳入
环境
可能是不可能的(这是鸡和蛋)。打破这种循环的一种方法是在父上下文中初始化数据库和存储库,然后在子上下文(主应用程序上下文)的
环境中使用它。在
SpringApplicationBuilder

中有一些方便的方法可以构建父上下文和子上下文,所有这些方法都只适用于数据库中的配置。这让我的懒惰开发人员感到畏缩:(懒惰很好。它是一种锅炉板模式,所以可能应该在Spring Boot中使用(在github中很容易提出问题,发送拉请求也几乎一样容易)。
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);
}