多Profile文件配置方式

在企业实际开发中,环境一般都有多个 ,如开发环境、测试环境、仿真环境、线上环境。不同环境的需要参数都是不一样的,比如数据库连接信息、redis服务器地址等,这时候就需要配置不同环境的配置。 1.yml多文档块方式

[Java] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
#默认配置,如果没有指定profiles,则默认配置生效
logging:
  level:
    cn.itcast: debug
    org.springframework: debug
spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot
    username: root
    password: 123456
mybatis:
  configuration:
    #查看运行Sql
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # mapper.xml文件位置,如果没有映射文件,请注释掉
  mapper-locations: classpath:mappers/*.xml
person:
  name: 小姐姐2
  age: 18
  mary: false
  birthday: 2001/03/27
  hobby:
    - 王者荣耀
    - 吃鸡
    - 看片
  sanwei: {height: 177cm,weight: 50cm, bust: 65cm }
  cat:
    name: 小花2
    age: 3
server:
  port: 80
 
---
#dev开发环境配置
logging:
  level:
    cn.itcast: debug
    org.springframework: debug
spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot
    username: root
    password: 123456
  profiles: dev  #指定属于dev开发环境
mybatis:
  configuration:
    #查看运行Sql
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # mapper.xml文件位置,如果没有映射文件,请注释掉
  mapper-locations: classpath:mappers/*.xml
person:
  name: 小姐姐1
  age: 18
  mary: false
  birthday: 2001/03/27
  hobby:
    - 王者荣耀
    - 吃鸡
    - 看片
  sanwei: {height: 177cm,weight: 50cm, bust: 65cm }
  cat:
    name: 小花1
    age: 4
server:
  port: 8081
 
---
#test测试环境配置
logging:
  level:
    cn.itcast: debug
    org.springframework: debug
spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot
    username: root
    password: 123456
  profiles: test #指定属于测试环境
mybatis:
  configuration:
    #查看运行Sql
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # mapper.xml文件位置,如果没有映射文件,请注释掉
  mapper-locations: classpath:mappers/*.xml
person:
  name: 小姐姐2
  age: 18
  mary: false
  birthday: 2001/03/27
  hobby:
    - 王者荣耀
    - 吃鸡
    - 看片
  sanwei: {height: 177cm,weight: 50cm, bust: 65cm }
  cat:
    name: 小花2
    age: 3
server:
  port: 8082
---
#启用环境
spring:
  profiles:
    active: dev


2 .多Profile文件方式
我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml
多Profile文件配置方式 
3 .**指定profile
  1、在配置yml文件中指定 spring.profiles.active=dev  2、命令行:  java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;  可以直接在测试的时候,配置传入命令行参数3、虚拟机参数;  -Dspring.profiles.active=dev
多Profile文件配置方式