弹簧靴始终使用相同的配置文件 [英] spring boot always using the same profile

查看:122
本文介绍了弹簧靴始终使用相同的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot 1.5.2和配置文件,但是发现了一件很奇怪的事情.

I am using spring boot 1.5.2, and using profiles but I found a very strange thing.

我的spring boot resources文件夹是这样的:

my spring boot resources folder like this:

application.yml中的配置

configs in application.yml

spring:
  profiles:
    active: @profileActive@

application-dev.yml

application-dev.yml

spring:
  profiles: dev
    datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/db1
      username: root
      password:
server:
  port: 8080

application-test.yml

application-test.yml

spring:
  profiles: test
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2
    username: root
    password:

server:
  port: 8081

我的pom.xml,仅包含资源部分和配置文件部分.

my pom.xml, just only include resources part and profile part.

<!-- profile -->
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>dev</build.profile.id>
            <profileActive>dev</profileActive>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <build.profile.id>test</build.profile.id>
            <profileActive>test</profileActive>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <build.profile.id>prod</build.profile.id>
            <profileActive>prod</profileActive>
        </properties>
    </profile>
</profiles>


<resources>
        <resource>
            <directory>src/main/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>application-${profileActive}.yml</include>
                <include>application.yml</include>
                <include>templates/*</include>
            </includes>
        </resource>

    </resources>

我现在正尝试使用测试配置文件,发现一切正常,@profileActive@已替换为test;

I am now trying to use test profile , found that everything is oK , @profileActive@ has been replace to test;

mvn clean package -Dmaven.test.skip=true -Ptest

看起来一切都很好.

但是当我尝试运行jar时,它总是使用 dev 配置文件,尽管application.yml显示我们现在使用的是test or prod配置文件.

but when I try to run the jar, it always using dev profile, although application.yml shows we now using test or prod profile.

我不知道yml配置中哪里出了问题.我尝试将所有配置文件配置仅包含在一个application.yml文件中.但是应用程序仍使用dev配置文件.

I don't know where is wrong in my yml configs. And I try to include all the profile configs in just one application.yml file. But the application still using dev profile.

在一个application.yml文件中完全配置

fully configs in one application.yml file

spring:
  profiles:
    active: @profileActive@

---
spring:
  profiles: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db1
  username: root
  password:

server:
  port: 8080

---
spring:
  profiles: test
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2
    username: root
    password:

server:
  port: 8081

---
spring:
  profiles: prod

server:
  port: 9000

最后,我尝试使用 properties 文件,我的所有配置都能正常运行,当我运行我的应用程序时可以使用正确的配置文件.

finally, I try to using properties files, all of my configs works fine, when I run my application can use the right profile.

现在,我只想知道yml配置有什么问题.

And Now, I just want to know what's wrong with my yml configs.

提前谢谢!

推荐答案

最后,我找到了原因.

我的项目是一个多模块maven项目.

my project is a multiple module maven project。

我正在使用yml格式的一个模块

one module I am using yml format,

和其他为property格式.

当我将两个模块都设置为yml格式时,一切正常.

when I make the two module both the yml format, everything ok.

谢谢大家!谢谢!

这篇关于弹簧靴始终使用相同的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆