springboot maven多环境配置yml实例
更新时间:2026年07月02日 10:44:19 作者:小鸡辛苦啄米
本文分享Maven环境配置技巧,利用application.yml与application-dev.yml实现多环境切换,只需勾选相应配置文件,即可统一线上代码,轻松区分开发与生产环境,解决线上多环境部署难题,提升开发效率
配置maven环境
<profiles>
<profile>
<id>dev</id>
<properties>
<package.environment>dev</package.environment>
</properties>
<!-- 是否默认 true表示默认-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<package.environment>test</package.environment>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>qa</id>
<properties>
<package.environment>qa</package.environment>
</properties>
</profile>
</profiles>`资源文件

application.yml
spring:
profiles:
active: @package.environment@application-dev.yml
spring:
datasource:
type: org.springframework.jdbc.datasource.DriverManagerDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.20.252/shlaunch?useUnicode=true&characterEncoding=utf-8&useSSL=true&autoReconnect=true
username: shlaunch
password: password
jpa:
database: mysql
show-sql: true
generate-ddl: true
database-platform: org.hibernate.dialect.MySQL5Dialect
hibernate:
ddl-auto: update
server:
port: 8081
logging:
level:
root: info
pattern:
console: '%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
spring:
mvc:
static-path-pattern: /**线上代码就不贴出来了 都是一样的
执行maven勾选中的yml
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>application-${package.environment}.yml</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
浅谈Java中Collection和Collections的区别
下面小编就为大家带来一篇浅谈Java中Collection和Collections的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-08-08
Spring Boot读取配置文件内容的3种方式(@Value、Environment和@ConfigurationP
工作中经常会有一些参数需要配置,同时在代码里面需要用到,所有就需要配置类读取,然后在使用的时候注入该类进行获取相关参数,下面这篇文章主要给大家介绍了关于Spring Boot读取配置文件内容的3种方式,需要的朋友可以参考下2023-01-01
Java并发编程ReentrantReadWriteLock加读锁流程
这篇文章主要介绍了Java并发编程ReentrantReadWriteLock加读锁流程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-05-05


最新评论