Spring-boot中利用外部設定檔產生資料來源__Java

來源:互聯網
上載者:User

Spring data 提供了一種很強大的JPA(主要是不需要給方法寫implements)
Spring boot則提供了方便的自動設定。 netgloo 的例子 如何只利用一個單一的設定檔 application.properties 資料訪問功能。
不過有時,你需要的是datasource是外部可配置的,而不是寫死在project中的。
所以,這裡博主便提供一種通過外部設定檔產生資料來源來替換Spring boot自動產生的資料來源。 第一步

在工程中產生一個Spring boot預設需要的設定檔: src/main/resources/application.properties

    spring.datasource.url = jdbc:postgresql://localhost:5432/bmsc    spring.datasource.username = aere    spring.datasource.password = aerexu    spring.datasource.testWhileIdle = true    spring.datasource.validationQuery = SELECT 1    spring.jpa.properties.datasource.driver-class-name=org.postgresql.Driver    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL9Dialect    # Show or not log for each sql query    spring.jpa.show-sql = true    # Hibernate ddl auto (create, create-drop, update)    spring.jpa.hibernate.ddl-auto = update    # Naming strategy    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
第二步

在其他路徑產生一個需要的設定檔C:\Users\test\Workplace\config\SpringAll\datasource.properties

spring.datasource.url = jdbc:postgresql://192.168.99.100:5432/bmscspring.datasource.username = aerespring.datasource.password = aerexu
最後一步

產生一個Spring的配置類 PersistenceJPAConfig

    package com.aere.spring.all.config.jpa;    // Imports ...    @Configuration    @ComponentScan    @PropertySource(value = {"classpath:/application.properties",            "file:/C:\\Users\\test\\Workplace\\config\\SpringAll\\datasource.properties"},            ignoreResourceNotFound = true)    public class PersistenceJPAConfig {        @Bean        @ConfigurationProperties(prefix="spring.datasource")        public DataSource dataSource() {            return new DriverManagerDataSource();        }    }

你可能會注意到在@PropertySource中有兩個設定檔。當然,實際有限的只會是一個。如果兩個檔案都存在,則後面一個檔案有效;如果只有一個檔案存在,當然是存在的那個有限。這麼做的目的的便於在開發中使用工程中的預設的設定檔,而在測試、部署中利用外部的設定檔。

好啦,這就是所有步驟了。是不是很簡單,只用了一些小技巧。無論如何,簡潔有效代碼才是最好的。 另外:

如果你需要顯示出來的密碼是加密過的,那你可以自訂一個Datasource類繼承上面的 DriverManagerDataSource. 如下所示:

public class EncryptedDriverManagerDataSource extends DriverManagerDataSource{    ...    public EncryptedDriverManagerDataSource(String url, String username, String password) {        setUrl(url);        setUsername(username);        String decryptedPass = someDecryptMethod(password);        setPassword(decryptedPass);    }    ...}

把上面類 PersistenceJPAConfigDriverManagerDataSource 替換成 EncryptedDriverManagerDataSource

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.