2017-02-09 42 views
0

我對Spring Framework非常陌生。如何從mysql數據庫讀取屬性而不是yml文件

在應用程序中,我們使用@Value spring註釋從application-dev.yml/application-prod.yml等中讀取屬性。

我想要做的是,我想將所有屬性從ymls移動到mysql數據庫並從mysql db訪問屬性。

我stucked這裏沒有發現任何方式來讀取分貝值..沒有春天有任何Annotation類似@Value從數據庫訪問值

你能建議我的方式來實現這一目標?還有沒有可用的參考碼?鏈接,博客到有關的主題表示讚賞。

感謝, SAURABH

+0

待辦事項你不知道如何從彈出的數據庫中讀取值,或者只是關於你的屬性nt閱讀? –

回答

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

相關問題