我使用弹簧引导1.5.2,并使用配置文件,但我发现了一件很奇怪的事情。弹簧引导始终使用相同的配置文件
CONFIGS在application.yml
spring:
profiles:
active: @[email protected]
应用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
应用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,只包含资源部分和配置文件部分。
<!-- 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>
我现在想用测试概,发现一切正常,@[email protected]
已经更换到test
;
mvn clean package -Dmaven.test.skip=true -Ptest
看起来好像一切正常。
但是当我尝试运行jar,它总是使用开发轮廓,虽然application.yml
显示了我们现在使用的test or prod
轮廓。
我不知道哪里是错误的,我阳明CONFIGS。我尝试将所有配置文件配置包含在一个application.yml文件中。但应用程序仍然使用dev
配置文件。
完全CONFIGS在一个application.yml文件
spring:
profiles:
active: @[email protected]
---
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
最后,我尝试使用性能文件,所有我的configs的正常工作,当我运行我的应用程序可以使用正确的个人资料。
而现在,我只想知道我的yml配置有什么问题。
在此先感谢!
你可以把所有的东西放在一个Git仓库中,让你很容易看到_exactly_你如何配置东西? –
我刚刚创建了一个与您定义的完全相同的简单项目。一切工作如预期。你可以尝试在intelliJ之外运行jar吗? – Patrick