【问题标题】:spring boot - last profile always usedspring boot - 始终使用最后一个配置文件
【发布时间】:2017-10-26 14:38:46
【问题描述】:

Spring boot 始终选择我的 application.yml 文件中的最后一个配置文件,无论我如何订购它们。请帮忙。如果我再把头发扯掉,我就没有了。

  • 使用 spring-boot-starter-parent 1.5.1.RELEASE
  • Maven 3.2.5
  • 我的工件中只有一个 application.yml。
  • 我在日志中看到:o.s.boot.SpringApplication.logStartupProfileInfo 641 - 以下配置文件处于活动状态:DEV

这是我的 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

万分感谢——感谢任何能提供见解的人。

【问题讨论】:

  • 看起来很奇怪,如果您有执行器依赖项,您是否通过 /env 端点验证了活动配置文件?

标签: java spring maven spring-boot


【解决方案1】:

我无法弄清楚是什么导致了这个问题。我不得不做一个解决方法。我切换到使用属性文件而不是 yaml。我为每个环境使用了一个单独的属性文件,然后为环境显式加载了适当的属性。我必须为我的 datasourceConfig.java 执行此操作。不理想,但确实有效。

String env1[] = this.environment.getActiveProfiles();
InputStream propertiesFile = DatasourceConfig.class.getClassLoader()
   .getResourceAsStream("application-" + env1[0] + ".properties");
prop.load(propertiesFile);

【讨论】:

  • 您是否尝试过我在其中一个答案中编写的 YAML 文件?我认为您缺少的是 profiles: active: default 设置
【解决方案2】:

这个 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 容器等)。

【讨论】:

  • 感谢您的建议。我的 yaml 本来是比较精简的,但是当我抓住稻草寻求解决方案时,我改变了它。
猜你喜欢
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 2017-02-24
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多