2017-05-25 56 views
0

无论我如何命令它们,Spring引导始终选择我的application.yml文件中的最后一个配置文件。请帮忙。如果我掏出更多的头发,我什么也没有留下。弹簧启动 - 始终使用的最后一个配置文件

  • 使用弹簧引导启动父1.5.1.RELEASE
  • 的Maven 3.2.5
  • 只有一个在我的神器application.yml。
  • 我在日志中看到这一点:osboot.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

千谢谢你给任何人提供见解。

+0

似乎很奇怪,如果您具有执行器相关性,您是否通过/ env端点验证了活动配置文件? –

回答

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

+0

谢谢你的建议。我的洋芋原本更加浓缩,但当我抓着吸管解决问题时,我改变了它。 – Eric

0

我找不出是什么原因造成了这个问题。我不得不做一个解决方法。我转而使用属性文件而不是yaml。我为每个环境使用了一个单独的属性文件,然后明确加载了相应的环境属性。我必须为我的datasourceConfig.java执行此操作。不理想,但它工作。

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

您是否尝试了我在其中一个答案中编写的YAML文件?我想你缺少的是'profiles:active:default'设置 –