首页 > 解决方案 > 当我运行 springboot 应用程序时,它总是从错误的路径加载属性

问题描述

背景:

我们有一个多模块项目,它由“ assembly ”、“ configservice ”、“ adminservice ”和“ portal ”四个模块组成。模块之间的依赖关系是这样的,“configservice”、“adminservice”和“portal”都是“程序集”的依赖关系。“程序集”所做的只是确定要运行哪个应用程序(“门户”、“管理服务”和“配置服务”)。

情况:

当我选择运行门户时,它总是从 configservice 而不是门户加载 bootstrap.yml。它还从 configservice 的类路径而不是从门户加载其他资源(如 liquibase 的属性)。
AssemblyApplication.java .assembly 的入口类,也是唯一的“assembly”类:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class})
public class AssemblyApplication {

  private static final Logger logger = LoggerFactory.getLogger(AssemblyApplication.class);

  public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext commonContext =
        new SpringApplicationBuilder(AssemblyApplication.class).web(false).run(args);
    commonContext.addApplicationListener(new ApplicationPidFileWriter());

    // Run configservice
    if (commonContext.getEnvironment().containsProperty("configservice")) {
      ConfigurableApplicationContext configContext =
          new SpringApplicationBuilder(ConfigServiceApplication.class).parent(commonContext)
              .sources(RefreshScope.class).run(args);
    }

    // Run adminservice
    if (commonContext.getEnvironment().containsProperty("adminservice")) {
      ConfigurableApplicationContext adminContext =
          new SpringApplicationBuilder(AdminServiceApplication.class).parent(commonContext)
              .sources(RefreshScope.class).run(args);
    }

    // Run portal
    if (commonContext.getEnvironment().containsProperty("portal")) {
      ConfigurableApplicationContext portalContext =
          new SpringApplicationBuilder(PortalApplication.class).parent(commonContext)
              .sources(RefreshScope.class).run(args);
    }
  }
}

文件结构:

configservice:
  -configservice
    -src
      -main
        -java
        -resources
          -liquibase
            -changelog.xml
        -application.yml
        -bootstrap.yml

adminservice:
  -adminservice
    -src
      -main
        -java
        -resources
          -application.yml
          -bootstrap.yml

portal:
  -portal
    -src
      -main
        -java
        -resources
          -liquibase
            -changelog.xml
        -application.yml

属性内容

configservice 的 bootstrap.yml
endpoints:
  health:
    sensitive: false

management:
  security:
    enabled: false
  health:
    status:
      order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP

spring:
  datasource:
    continue-on-error: true
    platform: h2
  jpa:
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: none


liquibase:
  change-log: classpath:liquibase/changelog.xml
  user:
  password:
  url: 
${spring_datasource_url:jdbc:h2:~/.h2/default/configdb;AUTO_SERVER=TRUE}
  enabled: true
  drop-first: false
configservice 的 application.yml
spring:
  application:
    name: configservice
  profiles:
    active: ${active_profile}

server:
  port: ${config_port:8330}

logging:
  file: /opt/logs/configservice.log
门户的 application.yml
spring:
  application:
    name: portal
  profiles:
    active: ${active_profile}
  datasource:
    continue-on-error: true
    platform: h2
  jpa:
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: none

server:
  port: 9080

logging:
  file: /opt/logs/100003173/portal.log

endpoints:
  health:
    sensitive: false
management:
  security:
    enabled: false
  health:
    status:
      order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP

liquibase:
  change-log: classpath:liquibase/changelog.xml
  user:
  password:
  url: ${spring_datasource_url}
  enabled: true
  drop-first: false

标签: mavenspring-bootliquibasemulti-module

解决方案


configservice.jar(或构建的工件所命名的任何名称)是类路径中的第一个并且获胜。

您应该将您的配置外部化或在每个模块中以不同的方式命名您的属性文件。

如何使用其他名称,您可以在此处找到:

http://roufid.com/rename-spring-boot-application-properties/


推荐阅读