springboot 2.0.4.RELEASEアプリのデータベース プロパティを使用して構成しようとしています。次のように新しい構成ファイルを追加しました。
@Configuration
@ConfigurationProperties(prefix="spring.datasource")
public class DatabaseConfig
{
private String userName;
private String password;
public String getUserName() {
try {
userName = Files.readAllLines(Paths.get("/secrets/username.txt")).get(0);
} catch (IOException e) {
e.printStackTrace();
}
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
try {
password = Files.readAllLines(Paths.get("/secrets/password.txt")).get(0);
} catch (IOException e) {
e.printStackTrace();
}
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
これが私のapplication.properties
spring.datasource.url=jdbc:sqlserver://mno35978487001.cloud.xyz.com:14481
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
アプリケーションが実行されたときに、ファイルからユーザー名とパスワードを読み取ることができることを望んapplication.propertiesでいましたが、それは起こらないようです。私が欠けているものについて何か考えはありますか?これらの値を読み取り、動的に設定するより良い方法はありますか?