springboot中.yml文件參數(shù)的讀取方式
yml文件參數(shù)的讀取
附上一個(gè)較為常見(jiàn)的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ù)庫(kù)連接地址 # username: root # 數(shù)據(jù)庫(kù)用戶名 # password: root # 數(shù)據(jù)庫(kù)連接密碼 # dbcp2: # 進(jìn)行數(shù)據(jù)庫(kù)連接池的配置 # min-idle: 5 # 數(shù)據(jù)庫(kù)連接池的最小維持連接數(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 # 超過(guò)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ù)庫(kù)連接池監(jiān)控統(tǒng)計(jì)插件 web-stat-filter: enabled: true url-pattern: /* # 過(guò)濾掉如下請(qǐng)求 exclusions: '*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/*' # 數(shù)據(jù)庫(kù)連接池監(jiān)控頁(yè)面插件 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 # 分頁(yè)配置 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中
可以通過(guò)@Value注解機(jī)型獲取
?@Value("${jwt.header}") ? private String token_header;
但是在service,serviceImpl等層中,沒(méi)辦法通過(guò)這種方式獲取,可以通過(guò)另外一種方式進(jìn)行獲取
通過(guò)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中通過(guò)@Autowired加載即可
?@Autowired ? ? private WxTemplateProperties wxTemplateProperties;
關(guān)于yml文件書寫的注意事項(xiàng)
現(xiàn)在大家發(fā)現(xiàn),在springboot里還是要用到配置文件的。 除了使用.properties外,springboot還支持 yml格式。
個(gè)人覺(jué)得yml格式的可讀性和..properties比起來(lái)差不多,有時(shí)候還沒(méi)有不如properties 看起來(lái)那么規(guī)整。
但是考慮到很多springboot項(xiàng)目會(huì)使用yml格式,還是簡(jiǎn)單講講,主要目的還是為了讀懂其他人的項(xiàng)目。
如圖所示,左邊是application.properties的寫法,右邊是application.yml的寫法,他們達(dá)到的效果是相同的
在application.yml 文件書寫注意:
1. 不同“等級(jí)” 用冒號(hào)隔開(kāi)
2. 次等級(jí)的前面是空格,不能使用制表符(tab)
3. 冒號(hào)之后如果有值,那么冒號(hào)和值之間至少有一個(gè)空格,不能緊貼著
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中@PathVariable和@RequestParam注解的用法區(qū)別
這篇文章主要介紹了Spring中@PathVariable和@RequestParam注解的用法區(qū)別,@PathVariable 是 Spring 框架中的一個(gè)注解,用于將 URL 中的變量綁定到方法的參數(shù)上,它通常用于處理 RESTful 風(fēng)格的請(qǐng)求,從 URL 中提取參數(shù)值,并將其傳遞給方法進(jìn)行處理,需要的朋友可以參考下2024-01-01Spring中的策略模式簡(jiǎn)單實(shí)現(xiàn)與使用分析
這篇文章主要介紹了Spring中的策略模式簡(jiǎn)單實(shí)現(xiàn)與使用分析,去初始化時(shí)除了?initMultipartResolver(上傳文件)沒(méi)有獲取?Properties?defaultStrategies;默認(rèn)策略,其他的八大件都會(huì)使用到策略模式,需要的朋友可以參考下2024-01-01Spring Boot項(xiàng)目@RestController使用重定向redirect方式
這篇文章主要介紹了Spring Boot項(xiàng)目@RestController使用重定向redirect方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09解決使用@ManyToMany查詢數(shù)據(jù)時(shí)的死循環(huán)問(wèn)題
這篇文章主要介紹了解決使用@ManyToMany查詢數(shù)據(jù)時(shí)的死循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java項(xiàng)目實(shí)現(xiàn)圖片等比縮放
這篇文章主要為大家詳細(xì)介紹了java項(xiàng)目實(shí)現(xiàn)圖片等比縮放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04SpringBoot Mybatis動(dòng)態(tài)數(shù)據(jù)源切換方案實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換方案過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04