Java Spring boot未加载特定配置文件

Java Spring boot未加载特定配置文件,java,spring,maven,spring-boot,spring-boot-maven-plugin,Java,Spring,Maven,Spring Boot,Spring Boot Maven Plugin,我无法从命令行加载特定的Spring启动配置文件 applciation.yml文件内容如下,它位于我的应用程序的资源文件夹中 server: port: 8787 spring: application: name: demo spring: profiles: local_mysql datasource: url: jdbc:mysql://localhost:3306/demo?createDatabaseIfNotExist=true us

我无法从命令行加载特定的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
java -jar -Dspring.profiles.active=local_mysql target\demo-1.0.0-SNAPSHOT.jar
执行
mvn清洁包后
并使用
java-jar-Dspring.profiles.active=local\u mysql target\demo-1.0.0-SNAPSHOT.jar


应用程序忽略指定的配置文件,只在8080上启动H2 Db而不是mySQL。

创建名为
Application-local\u mySQL.yml
的单独文件,并在该文件中具有
local\u mySQL
相关设置。对所有配置文件执行相同的操作。在
application.yml
中,所有配置文件都有相同的配置

文件应位于
$CLASSPATH\config\
位置

然后运行应用程序

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
java -jar -Dspring.profiles.active=local_mysql target\demo-1.0.0-SNAPSHOT.jar

Ref:

创建名为
application-local\u mysql.yml
的单独文件,并在该文件中具有
local\u mysql
相关设置。对所有配置文件执行相同的操作。在
application.yml
中,所有配置文件都有相同的配置

文件应位于
$CLASSPATH\config\
位置

然后运行应用程序

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
java -jar -Dspring.profiles.active=local_mysql target\demo-1.0.0-SNAPSHOT.jar

参考:

在我看来,最好为不同的配置文件创建多个yml文件(如@karthikeyan vaithilingam post中所述),但仅供注意-您可以在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}}

在我看来,最好为不同的配置文件创建多个yml文件(如@karthikeyan vaithilingam post中所述),但请注意-您可以在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}}