2016-11-07 5 views
2

データベースにSpringブートアプリケーションの設定を保存する必要があります。Spring Boot:設定にdatabaseとapplication.propertiesを使用する

データベース情報をapplication.propertiesに保存し、データベースに接続してそこから他のすべてのプロパティを取得することはできますか?

だから私のapplication.propertiesは次のようになります。

spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=mydb 
spring.datasource.username=user 
spring.datasource.password=123456 
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect 

そして、他の構成はこのようなもので、データベースからフェッチされます:EntityとしてApplicationConfiguration

@Configuration 
@PropertySource(value = {"classpath:application.properties"}) 
public class ConfigurationPropertySource { 

    private final ConfigurationRepository configurationRepository; 

    @Autowired 
    public ConfigurationPropertySource(ConfigurationRepository configurationRepository) { 
     this.configurationRepository = configurationRepository; 
    } 

    public String getValue(String key) { 
     ApplicationConfiguration configuration = configurationRepository.findOne(key); 
     return configuration.getValue(); 
    } 

} 

しかし、Springブートはデータベースから設定を取得しません。

+0

作成したとおりに、プロパティファイルからdb設定を取得できるはずです。コンソールにエラーが表示されますか? – kuhajeyan

+0

私は 'application.properties'から設定を取得します。そして私はデータベースに接続し、エンティティを保存することができます。しかし、データベースのプロパティは読み込まれません。 – deve

+0

次にConfigurationRepositoryは何をしますか? – kuhajeyan

答えて

2

解決策の1つは、ConfigurableEnvironmentを使用してプロパティを再読み込みして追加することです。

@Configuration 
public class ConfigurationPropertySource { 

private ConfigurableEnvironment env; 

private final ConfigurationRepository configurationRepository; 

    @Autowired 
    public ConfigurationPropertySource(ConfigurationRepository configurationRepository) { 
     this.configurationRepository = configurationRepository; 
    } 

    @Autowired 
    public void setConfigurableEnvironment(ConfigurableEnvironment env) { 

     this.env = env; 
    } 

    @PostConstruct 
    public void init() { 
    MutablePropertySources propertySources = env.getPropertySources(); 
     Map myMap = new HashMap(); 
     //from configurationRepository get values and fill mapp 
     propertySources.addFirst(new MapPropertySource("MY_MAP", myMap)); 
    } 

} 
+0

残念ながら、これは機能しませんでした。データベースには、SQLロギングの構成プロパティが含まれています。しかし、データベースにレコードを追加したときにログが表示されなかったので、プロパティがロードされていないとします。 – deve

+1

@deveプロパティはロードされましたが、ロギングが設定された後にロードされました。データベースのプロパティを使用してBeanを構成できます。私は '@ PostConstruct'ではなく、' DataSource'が開始された直後にカスタム 'BeanPostProcessor'でそれらを初期化することを好みますが、 –

+0

@EugeneToあなたに例を挙げてください。 – deve

0

残念ながら、この問題の解決策はまだありませんが、次の回避策を使用しています(設定変更時にアプリケーションを再起動する必要があります)。

@Component 
public class ApplicationConfiguration { 

    @Autowired 
    private ConfigurationRepository configurationRepository; 

    @Autowired 
    private ResourceLoader resourceLoader; 

    @PostConstruct 
    protected void initialize() { 
     updateConfiguration(); 
    } 

    private void updateConfiguration() { 
     Properties properties = new Properties(); 

     List<Configuration> configurations = configurationRepository.findAll(); 
     configurations.forEach((configuration) -> { 
      properties.setProperty(configuration.getKey(), configuration.getValue()); 
     }); 

     Resource propertiesResource = resourceLoader.getResource("classpath:configuration.properties"); 
     try (OutputStream out = new BufferedOutputStream(new FileOutputStream(propertiesResource.getFile()))) { 
      properties.store(out, null); 
     } catch (IOException | ClassCastException | NullPointerException ex) { 
      // Handle error 
     } 
    } 

} 

データベースから設定を読み込んで、別のプロパティファイルに書き込みます。このファイルは@PropertySource("classpath:configuration.properties")と一緒に使用できます。

関連する問題