2017-02-09 4 views
0

私はSpring Frameworkの新機能を大いに覚えています。ymlファイルの代わりにmysqlデータベースからプロパティを読み取る方法

アプリケーションでは、@Valueスプリングアノテーションを使用して、application-dev.yml/application-prod.ymlなどからプロパティを読み取ります。

私がしたいことは、ymlsのすべてのプロパティをmysqlデータベースに移動し、mysql dbのプロパティにアクセスしたいということです。

私がDBから値を読み取るためにどのような方法を見つけ、ここではなくstucked午前..春には、データベース

から値にアクセスするために@Valueに似たいかなるAnnotationを持っていないあなたは私にこれを達成するための方法を提案することはできますか?参照コードもありますか?関連するトピックへのリンク、ブログは高く評価されています。あなたが財産ローダの独自のカスタム実装を作成する必要があります

おかげで、 Saurabh

+0

ドゥあなたはgerneralで春のデータベースから値を読み取る方法を知らない、またはあなたが持っているプロパティについてだけである読み込むには? –

答えて

1

public class DbPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer { 

    private static final Logger LOG = LoggerFactory.getLogger(DbPlaceholderConfigurer.class); 

    public DbPlaceholderConfigurer(DataSource dataSource) { 
     try { 
      Map<String, Object> loadedSettings = loadProperties(dataSource); 
      MutablePropertySources mutablePropertySources = new MutablePropertySources(); 
      mutablePropertySources.addFirst(new MapPropertySource("custom", loadedSettings)); 
      setPropertySources(mutablePropertySources); 
     } catch (SQLException e) { 
      LOG.error("Error loading properties", e); 
     } 
    } 

    private Map<String, Object> loadProperties(DataSource dataSource) throws SQLException { 
     LOG.info("{}", dataSource); 
     Connection connection = dataSource.getConnection(); 
     PreparedStatement prepareStatement = connection.prepareStatement("SELECT * FROM Settings"); 
     Map<String, Object> loadedSettings = new HashMap<String, Object>(); 
     prepareStatement.execute(); 
     ResultSet resultSet = prepareStatement.getResultSet(); 
     while (resultSet.next()) { 
      loadedSettings.put(resultSet.getString(2), resultSet.getString(3)); 
     } 
     connection.close(); 
     return loadedSettings; 
    } 
} 

ここで設定クラスがあります。以下はHSQLで与えられ、必要に応じて変更します。 mySqlデータソース。

@Configuration 
@ComponentScan({ "package_name_to_scan" }) 
public class AppConfig { 

    private static final Logger LOG = LoggerFactory.getLogger(AppConfig.class); 

    private static EmbeddedDatabase dataSource; 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     return new DbPlaceholderConfigurer(dataSource()); 
    } 

    @Bean 
    public static DataSource dataSource() { 
     if(dataSource == null) { 
      EmbeddedDatabaseBuilder databaseBuilder = new EmbeddedDatabaseBuilder(); 
      databaseBuilder.addScript("classpath:schema.sql"); 
      databaseBuilder.setName("test"); 
      databaseBuilder.setType(EmbeddedDatabaseType.HSQL); 
      EmbeddedDatabase build = databaseBuilder.build(); 
      initPopulate(build); 
      dataSource = build; 
     } 
     return dataSource; 
    } 

    private static void initPopulate(EmbeddedDatabase embeddedDatabase) { 
     try { 
      Connection connection = embeddedDatabase.getConnection(); 
      PreparedStatement prepareStatement; 
      prepareStatement = connection.prepareStatement("INSERT INTO Settings VALUES (?,?,?)"); 
      prepareStatement.setInt(1, 1); 
      prepareStatement.setString(2, "testKey"); 
      prepareStatement.setString(3, "testVal"); 
      prepareStatement.executeUpdate(); 
      connection.close(); 
     } catch (SQLException e) { 
      LOG.error("Error ", e); 
     } 
    } 
} 

Schema.sql:

CREATE TABLE Settings(
    id INT PRIMARY KEY, 
    testKey VARCHAR(100), 
    testValue VARCHAR(100) 
); 

今すぐあなたのSpring Beanに以下のような@value使用することができます。

@Value("${testKey}") 
private String value; 

完全なソースコードはこちらです:https://github.com/szaqal/KitchenSink/tree/master/Java/Spring/propertiesload