springboot中.yml文件參數(shù)的讀取方式
yml文件參數(shù)的讀取
附上一個(gè)較為常見的application.yml文件示例
server:
port: 9999
use-forward-headers: true
tomcat:
remote-ip-header: X-Real-IP
protocol-header: X-Forwarded-Proto
spring:
# datasource:
# type: com.alibaba.druid.pool.DruidDataSource # 配置當(dāng)前要使用的數(shù)據(jù)源的操作類型
# driver-class-name: org.gjt.mm.mysql.Driver # 配置MySQL的驅(qū)動(dòng)程序類
# url: jdbc:mysql://127.0.0.1:3306/drink?useUnicode=true&characterEncoding=UTF-8 # 數(shù)據(jù)庫連接地址
# username: root # 數(shù)據(jù)庫用戶名
# password: root # 數(shù)據(jù)庫連接密碼
# dbcp2: # 進(jìn)行數(shù)據(jù)庫連接池的配置
# min-idle: 5 # 數(shù)據(jù)庫連接池的最小維持連接數(shù)
# initial-size: 5 # 初始化提供的連接數(shù)
# max-total: 5 # 最大的連接數(shù)
# max-wait-millis: 200 # 等待連接獲取的最大超時(shí)時(shí)間
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/drink-bar?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
# 初始化時(shí)建立的連接數(shù)
initial-size: 5
# 最大連接數(shù)
max-active: 20
# 最小連接數(shù)
min-idle: 5
# 獲取連接最大等待時(shí)間,單位:毫秒
max-wait: 2000
# 是否緩存preparedStatement
pool-prepared-statements: false
# 最大preparedStatement緩存數(shù),當(dāng)pool-prepared-statements=true時(shí)需要大于0
max-pool-prepared-statement-per-connection-size: -1
# 檢測(cè)連接是否失效的sql
validation-query: SELECT 'x'
# 檢測(cè)連接是否失效的超時(shí)時(shí)間,單位:秒
validation-query-timeout: 2
filters: stat,wall,log4j2,config
time-between-log-stats-millis: 604800000
# connection-properties: config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAI0Q56HOXI2Z33kNpgOxvMbaE7BRUw0pZMLPZA0Pe+J/6R7Cq97BUCDygkzL7evUfQmPprNh6KKaSKXV0dBVghcCAwEAAQ==
# Spring aop監(jiān)控的包路徑
aop-patterns: cn.greedystar.springbootdemo.modules.service.*
filter:
config:
enabled: true
# 監(jiān)控統(tǒng)計(jì)
stat:
enabled: true
db-type: mysql
# 打印慢sql
log-slow-sql: true
# 超過200毫秒即為慢sql
slow-sql-millis: 200
# sql防火墻
wall:
enabled: true
db-type: mysql
# 對(duì)認(rèn)定的攻擊sql進(jìn)行日志輸出
log-violation: true
# 對(duì)認(rèn)定的攻擊sql拋出異常
throw-exception: true
config:
# 是否允許下述操作
alter-table-allow: false
truncate-allow: false
drop-table-allow: false
update-where-none-check: true
# metadata會(huì)暴露數(shù)據(jù)的表結(jié)構(gòu)
metadata-allow: true
# 日志
log4j2:
enabled: true
# log4j2僅記錄druid的sql執(zhí)行日志
statement-log-enabled: false
connection-log-enabled: false
result-set-log-enabled: false
statement-executable-sql-log-enable: true
# 數(shù)據(jù)庫連接池監(jiān)控統(tǒng)計(jì)插件
web-stat-filter:
enabled: true
url-pattern: /*
# 過濾掉如下請(qǐng)求
exclusions: '*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/*'
# 數(shù)據(jù)庫連接池監(jiān)控頁面插件
stat-view-servlet:
enabled: true
url-pattern: '/druid/*'
reset-enable: true
login-username: admin
login-password: admin
allow:
deny:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 100MB
redis:
database: 2
host: www.mist1.com
port: 9000
password: mistone23s
jedis:
pool:
max-idle: 1000
max-wait: -1
min-idle: 2
max-active: 10
mybatis:
mapper-locations:
# - classpath:mapper/*.xml
- classpath*:mapper/*.xml
# 日志配置文件
logging:
config: classpath:log4j2/log4j2.xml
# 分頁配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
# JWT
jwt:
header: Authorization
secret: mySecret
#token有效期一天
expiration: 86400
tokenHead: "Bearer "
filePath: E://upload
fileServerUrl: http://file.mist1.com
env: dev1
dada:
shopNo: 21443-254867
sourceId: 21443
isPrepay: 1
debug: true
wx:
template-msg:
appid: wx092f89864f7afb50
take_out_success: HS5BpCEGQkf-6fKjn0YeVKpEfqe_5WzmaGgNX1-sdT4
self_take_success: HS5BpCEGQkf-6fKjn0YeVIEfy8nc_ZIm0QkqhTcBg2g
remind_take: E7Oe-OBFh8ABAWudkti1MTUB7mXOOPTZoGTr5J0Um4Y
order_delivery: rYSNmi-tn-HS_68S2vmFrTlfbgD-MhWse3RS21GSFWs
evaluate_wait: kZg0wJ8cXDSQVlWBxVkNbdHSvF8_tEZRoKf0yzQCUTQ
ticketUrl: http://localhost:9999/api/v1/print/printTicketByJson
tipUrl: http://localhost:9999/api/v1/print/printTipByJson
miniapp:
configs:
- appid: #微信小程序appid
secret: #微信小程序secret
token: #微信小程序消息服務(wù)器配置的token
aesKey: #微信小程序消息服務(wù)器配置的EncodingAESKey
msgDataFormat: JSON
- appid: #微信小程序appid
secret: #微信小程序secret
token: #微信小程序消息服務(wù)器配置的token
aesKey: #微信小程序消息服務(wù)器配置的EncodingAESKey
msgDataFormat: JSON
pay:
appId: #微信小程序APPID
mchId: #商戶號(hào)
mchKey: #商戶號(hào)秘鑰
subAppId: #服務(wù)商模式下的子商戶公眾賬號(hào)ID
subMchId: #服務(wù)商模式下的子商戶號(hào)
keyPath: classpath:cert/apiclient_cert.p12
notifyUrl: http://wx.mist1.com/wx/api/pay/notify/order
refundUrl: http://wx.mist1.com/wx/api/pay/notify/refund
tradeType: JSAPI
正常在controller中
可以通過@Value注解機(jī)型獲取
?@Value("${jwt.header}")
? private String token_header;但是在service,serviceImpl等層中,沒辦法通過這種方式獲取,可以通過另外一種方式進(jìn)行獲取
通過config文件的讀取
package com.drink.admin.core.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "wx.template-msg")
public class WxTemplateProperties {
private String appid;
private String take_out_success;
private String self_take_success;
private String remind_take;
private String order_delivery;
private String evaluate_wait;
private String ticketUrl;
private String tipUrl;
}使用@ConfigurationProperties注解讀取
在service中通過@Autowired加載即可
?@Autowired ? ? private WxTemplateProperties wxTemplateProperties;
關(guān)于yml文件書寫的注意事項(xiàng)
現(xiàn)在大家發(fā)現(xiàn),在springboot里還是要用到配置文件的。 除了使用.properties外,springboot還支持 yml格式。
個(gè)人覺得yml格式的可讀性和..properties比起來差不多,有時(shí)候還沒有不如properties 看起來那么規(guī)整。
但是考慮到很多springboot項(xiàng)目會(huì)使用yml格式,還是簡(jiǎn)單講講,主要目的還是為了讀懂其他人的項(xiàng)目。
如圖所示,左邊是application.properties的寫法,右邊是application.yml的寫法,他們達(dá)到的效果是相同的

在application.yml 文件書寫注意:
1. 不同“等級(jí)” 用冒號(hào)隔開
2. 次等級(jí)的前面是空格,不能使用制表符(tab)
3. 冒號(hào)之后如果有值,那么冒號(hào)和值之間至少有一個(gè)空格,不能緊貼著
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC自定義消息轉(zhuǎn)換器的使用其實(shí)很簡(jiǎn)單
這篇文章主要介紹了SpringMVC自定義消息轉(zhuǎn)換器的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
java如何用Processing生成馬賽克風(fēng)格的圖像
這篇文章主要介紹了如何用java如何用Processing生成馬賽克風(fēng)格的圖像,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
聊聊springmvc中controller的方法的參數(shù)注解方式
本篇文章主要介紹了聊聊springmvc中controller的方法的參數(shù)注解方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Spring?Security如何實(shí)現(xiàn)升級(jí)密碼加密方式詳解
這篇文章主要為大家介紹了Spring?Security實(shí)現(xiàn)升級(jí)密碼加密方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Spring中利用SchedulingConfigurer實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)配置的示例
定時(shí)任務(wù)是一項(xiàng)至關(guān)重要的功能,它們使得我們能夠按照預(yù)定的時(shí)間執(zhí)行特定的任務(wù),本文主要介紹了Spring中利用SchedulingConfigurer實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)配置的示例,感興趣的可以了解一下2024-05-05
解讀CommandLineRunner或者ApplicationRunner接口
這篇文章主要介紹了解讀CommandLineRunner或者ApplicationRunner接口的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
深入淺析Spring 的aop實(shí)現(xiàn)原理
AOP(Aspect-OrientedProgramming,面向方面編程),可以說是OOP(Object-Oriented Programing,面向?qū)ο缶幊蹋┑难a(bǔ)充和完善。本文給大家介紹Spring 的aop實(shí)現(xiàn)原理,感興趣的朋友一起學(xué)習(xí)吧2016-03-03
UniApp?+?SpringBoot?實(shí)現(xiàn)支付寶支付和退款功能
這篇文章主要介紹了UniApp?+?SpringBoot?實(shí)現(xiàn)支付寶支付和退款功能,基本的?SpringBoot?的腳手架,可以去IDEA?自帶的快速生成腳手架插件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06
Java 編程如何使用 Class.forName() 加載類
在一些應(yīng)用中,無法事先知道使用者將加載什么類,而必須讓使用者指定類名稱以加載類,可以使用 Class的靜態(tài)forName()方法實(shí)現(xiàn)動(dòng)態(tài)加載類,這篇文章主要介紹了Java編程如何使用Class.forName()加載類,需要的朋友可以參考下2022-06-06

