簡體   English   中英

Spring Boot未加載特定配置文件

[英]Spring boot not loading specific profile

我無法從命令行加載特定的Spring引導配置文件。

applciation.yml文件的內容如下,並將其放在我的應用程序的資源文件夾中。

server:
    port: 8787
spring:
  application:
    name: demo

spring:
  profiles: local_mysql
  datasource:
    url: jdbc:mysql://localhost:3306/demo?createDatabaseIfNotExist=true
    username: root
    password: root
    driverClassName: com.mysql.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
      dialect: org.hibernate.dialect.MySQLDialect
server:
    port: 8787

spring:
  profiles: development
  datasource:
    url: jdbc:mysql://localhost:3306/demo?createDatabaseIfNotExist=true
    username: admin
    password: admin
    driverClassName: com.mysql.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
      dialect: org.hibernate.dialect.MySQLDialect
server:
    port: 8788

在執行mvn clean package並使用java -jar -Dspring.profiles.active=local_mysql target\\demo-1.0.0-SNAPSHOT.jar運行應用程序之后

應用程序將忽略指定的配置文件,而僅在8080上以H2 Db代替mySQL。

創建一個名為application-local_mysql.yml單獨文件,並在該文件中進行與local_mysql相關的設置。 對所有配置文件執行相同的操作。 application.yml具有所有配置文件共有的配置。

文件應位於$CLASSPATH\\config\\位置。

然后運行您的應用程序。

java -jar -Dspring.profiles.active=local_mysql target\demo-1.0.0-SNAPSHOT.jar

參考: 外部化配置

在我看來,最好為不同的配置文件創建許多yml文件(如@ karthikeyan-vaithilingam文章中所述),但僅作說明-您可以在application.yml中擁有多個配置文件的屬性-這里的eureka使用示例:

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
    metadataMap:
      # Each eureka instance need unique id. By default its hostname so we would have to use 1 server per service
      instanceId: PEER1_${spring.application.name}:${spring.application.instance_id:${random.value}}
---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
    metadataMap:
  # Each eureka instance need unique id. By default its hostname so we would have to use 1 server per service
      instanceId: PEER2_${spring.application.name}:${spring.application.instance_id:${random.value}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM