0
点赞
收藏
分享

微信扫一扫

#yyds干货盘点#Maven中Profiles的配置

龙驹书房 2022-02-10 阅读 112

个人博客: javalover.cc

前言

平时在开发过程中,本地和生产环境的配置都是不一样的,比如数据库配置,日志打印等等;

这样就会有两种配置文件:开发环境和生产环境

下面我们用例子来介绍下,如何配置不同的环境,以及如何在他们之间进行切换

步骤

  1. 先创建三个配置文件,一个基本配置,一个开发环境配置,一个生产环境配置
  2. 再通过active属性来激活不同的配置

1. 配置文件

application.yml

#服务配置
server:
  port: 8081
  max-http-header-size: 10240

#spring相关配置
spring:
  servlet:
    multipart:
      max-request-size: 100MB
      max-file-size: 100MB
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss.SSS
    locale: zh_CN
    serialization:
      # 格式化输出
      indent_output: false

application-dev.yml

# Mysql数据库
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
    username: root
    password: root
    # 连接池大小根据实际情况调整
    max-active: 20
    max-pool-prepared-statement-per-connection-size: 20

application-prod.yml

# Mysql数据库
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://www.javalover.cc:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
    username: root
    password: root
    # 连接池大小根据实际情况调整
    max-active: 20
    max-pool-prepared-statement-per-connection-size: 20

2. 激活

激活有两种

  • 一种就是直接在 application.yml 中用常量赋值,如下所示:
#spring相关配置
spring:
  # 增加了这两行代码,表示激活dev开发配置环境,即application-dev.yml 
  profiles:
    active: dev
  • 另一种就是从pom.xml中读取属性值,用变量赋值;

这种方式需要先在pom.xml中进行配置,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>javalover</artifactId>

    <packaging>jar</packaging>

    <build>
        <finalName>javalover</finalName>
        <!-- 1. 首先要配置过滤目录,即yml所在的目录 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <!-- 2. 其次再配置profile,这里配置了两个,一个dev,一个prod;后面的会覆盖前面的,所以这里生效的为dev -->
    <profiles>
        <profile>
            <id>prod</id>
            <!-- yml读取的属性就是下面<properties>中包含的属性,这里读取的属性为 spring.active -->
            <properties>
                <spring.active>prod</spring.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <id>dev</id>
            <!-- 后面的会覆盖前面的,所以这里生效的为dev -->
            <properties>
                <spring.active>dev</spring.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

    </profiles>
</project>

然后在yml中读取刚才配置的spring.active属性,读取格式为 @spring.active@

#spring相关配置
spring:
  profiles:
    active: @spring.active@

3. 启动测试

到这里基本就差不多了,可以启动测试,看下输出,如下所示:

2021-08-12 18:50:22.898  INFO 15268 --- [           main] cn.stylefeng.guns.GunsApplication        : The following profiles are active: dev

参考:https://maven.apache.org/guides/introduction/introduction-to-profiles.html

举报

相关推荐

0 条评论