当我运行springboot应用程序时,它总是从错误的路径加载属性

时间:2018-09-29 11:04:36

标签: maven spring-boot liquibase multi-module

背景:

我们有一个多模块项目,该项目包含四个模块“ 程序集”,“ configservice ”,“ adminservice ”和“ 门户” ”。模块之间的依赖关系如下,“ configservice”,“ adminservice”和“ portal”都是“ assembly”的依赖关系。“ assembly”的作用只是确定哪个应用程序(“ portal”, “ adminservice”和“ configservice”)运行。

情况:

当我选择运行 portal 时,它总是从configservice而不是从Portal加载bootstrap.yml。它还会从configservice的类路径而不是从Portal加载其他资源(例如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

1 个答案:

答案 0 :(得分:0)

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

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

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

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