【Groovy】Groovy 脚本调用 ( Groovy 配置文件格式 | Groovy 配置文件读取 | 完整配置文件及解析代码示例 )
生活随笔
收集整理的這篇文章主要介紹了
【Groovy】Groovy 脚本调用 ( Groovy 配置文件格式 | Groovy 配置文件读取 | 完整配置文件及解析代码示例 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 一、Groovy 配置文件格式
- 二、Groovy 配置文件讀取
- 二、完整配置文件及解析代碼示例
前言
在 Groovy 腳本 , Groovy 類 , Java 類中 , 可以調用 Groovy 腳本 ;
一、Groovy 配置文件格式
Groovy 中的配置文件 , 也是定義在 " .groovy " 腳本中的 ;
下面的寫法 ,
student {name = "Tom" }與
student.name = "Tom"用法相同 ;
同時在每個節點下 , 還可以創建子節點 , 子節點下配置鍵值對 ;
student {name = "Tom"home{address = "Beijing"} }二、Groovy 配置文件讀取
Groovy 配置文件讀取 , 創建 ConfigSlurper 對象 , 使用 parse 方法 , 解析指定的配置文件 , 注入傳入的是 URL 對象 ;
// 解析 Config.groovy 配置文件 def config = new ConfigSlurper().parse(new File("Config.groovy").toURI().toURL())之后可以使用 config.節點名稱.鍵 的形式 , 讀取配置文件 ;
如使用 config.student.name 形式 , 讀取 student 下的 name 屬性配置 ;
代碼示例 :
// 解析 Config.groovy 配置文件 def config = new ConfigSlurper().parse(new File("Config.groovy").toURI().toURL())// 打印 student 整個配置 println "student : " + config.student二、完整配置文件及解析代碼示例
配置文件 :
student {name = "Tom"age = 18school {address = "Beijing"name = "School"}home{address = "Beijing"} }解析配置文件代碼示例 :
// 解析 Config.groovy 配置文件 def config = new ConfigSlurper().parse(new File("Config.groovy").toURI().toURL())// 打印 student 整個配置 println "student : " + config.student// 打印 student 下的 name 和 age 配置 println "student.name : ${config.student.name} , student.age : ${config.student.age}"// 打印 student 下的 school 和 home 節點配置 println "student.school : " + config.student.school + " , student.home : " + config.student.home// 打印 student 下 school 下的 address 和 name 屬性 println "student.school.address : " + config.student.school.address + " , student.school.name : " +config.student.school.name執行結果 :
student : [name:Tom, age:18, school:[address:Beijing, name:School], home:[address:Beijing]] student.name : Tom , student.age : 18 student.school : [address:Beijing, name:School] , student.home : [address:Beijing] student.school.address : Beijing , student.school.name : School總結
以上是生活随笔為你收集整理的【Groovy】Groovy 脚本调用 ( Groovy 配置文件格式 | Groovy 配置文件读取 | 完整配置文件及解析代码示例 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Groovy】Groovy 脚本调用
- 下一篇: 【Groovy】Groovy 脚本调用