Spring boot一直在我的application.yml文件中选择最后一个配置文件,无论我如何订购它们。请帮忙。如果我再撕掉头发,我就没有了。
这是我的application.yml:
server:
context-path: /MyApplicationUI
port: 8480
---
# LOCAL
spring:
profiles: LOCAL
datasource:
driver-class-name: net.sourceforge.jtds.jdbc.Driver
dialect: org.hibernate.dialect.SQLServerDialect
username: #insert username#
encrypted-password: #insert password#
url: jdbc:jtds:sqlserver:blah blah stuff here;
jpa:
database-platform: org.hibernate.dialect.SQLServerDialect
show-sql: true
---
# DEVELOPMENT
spring:
profiles: DEV
datasource:
driver-class-name: net.sourceforge.jtds.jdbc.Driver
dialect: org.hibernate.dialect.SQLServerDialect
username: #insert username#
encrypted-password: #insert password#
url: jdbc:jtds:sqlserver:blah blah stuff here;
jpa:
database-platform: org.hibernate.dialect.SQLServerDialect
show-sql: true
---
# TEST
spring:
profiles: TEST
datasource:
driver-class-name: net.sourceforge.jtds.jdbc.Driver
dialect: org.hibernate.dialect.SQLServerDialect
username: #insert username#
encrypted-password: #insert password#
url: jdbc:jtds:sqlserver:blah blah stuff here;
jpa:
database-platform: org.hibernate.dialect.SQLServerDialect
show-sql: true
我正在通过我自己的DatasourceConfig.java加载加密密码:
public class DatasourceConfig {
@Value("${encrypted-password}")
private String encryptedPassword;
/**
* Sets up the datasource with Spring - decrypting password first
*
* @return Datasource
*/
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource setupDataSource() {
return DataSourceBuilder.create().password(getSecurePassword()).build();
}
/**
* Decrypts encryptedPassword property
*
* @return decryptedPassword
*/
private String getSecurePassword() {
System.out.println("Encrypted password = " + encryptedPassword);
return new AESEncryptionUtils().decryptString(encryptedPassword);
}
...
我没有多个模块:spring boot always using the same profile
万分感谢 - 任何人都可以提供见解。
答案 0 :(得分:0)
这个YAML文件看起来更简洁:
server:
context-path: /MyApplicationUI
port: 8480
spring:
datasource:
driver-class-name: net.sourceforge.jtds.jdbc.Driver
dialect: org.hibernate.dialect.SQLServerDialect
username: #insert username#
encrypted-password: #insert password#
url: jdbc:jtds:sqlserver:blah blah stuff here;
jpa:
database-platform: org.hibernate.dialect.SQLServerDialect
show-sql: true
profiles:
active: default, local
---
# DEVELOPMENT
spring:
profiles: DEV
datasource:
username: #insert username#
encrypted-password: #insert password#
url: jdbc:jtds:sqlserver:blah blah stuff here;
---
# TEST
spring:
profiles: TEST
datasource:
username: #insert username#
encrypted-password: #insert password#
url: jdbc:jtds:sqlserver:blah blah stuff here;
您不需要一直重复所有内容,只需在配置文件之间更改“部件”。默认情况下,使用此配置时,将使用的配置文件为:local
和/或default
。
如果你想使用另一个,你必须将这个开关--spring.profiles.active=DEV
(或你想要的标识符)传递给命令行上的工件(或脚本,Docker容器等)
答案 1 :(得分:0)
我无法弄清楚导致这个问题的原因。我不得不做一个解决方法。我切换到使用属性文件而不是yaml。我为每个环境使用了一个单独的属性文件,然后为环境显式加载了相应的属性。我必须为我的datasourceConfig.java做这个。不理想,但它有效。
String env1[] = this.environment.getActiveProfiles();
InputStream propertiesFile = DatasourceConfig.class.getClassLoader()
.getResourceAsStream("application-" + env1[0] + ".properties");
prop.load(propertiesFile);