多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
107java

默認配置,若是沒有指定profiles,則默認配置生效

logging:
level:mysql

cn.itcast: debug
org.springframework: debug

spring:
datasource:redis

driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot
username: root
password: 123456

mybatis:
configuration:spring

#查看運行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:sql

- 王者榮耀
- 吃雞
- 看片

sanwei: {height: 177cm,weight: 50cm, bust: 65cm }
cat:數據庫

name: 小花2
age: 3

server:
port: 80apache


dev開發環境配置

logging:
level:springboot

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:mybatis

#查看運行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

3 .激活指定profile 一、在配置yml文件中指定 spring.profiles.active=dev 二、命令行: java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev; 能夠直接在測試的時候,配置傳入命令行參數三、虛擬機參數; -Dspring.profiles.active=dev

相關文章
相關標籤/搜索