【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 = 18
    school {
        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


image.png



目录
相关文章
|
1天前
|
弹性计算 运维 Shell
每天解析一个shell脚本(61)
【4月更文挑战第26天】shell脚本解析及训练(61)
12 3
|
1天前
|
弹性计算 运维 Shell
每天解析一个shell脚本(58)
【4月更文挑战第26天】shell脚本解析及训练(58)
67 0
|
1天前
|
弹性计算 Shell 数据安全/隐私保护
每天解析一个shell脚本(56)
【4月更文挑战第26天】shell脚本解析及训练(56)
13 0
|
1天前
|
弹性计算 运维 监控
每天解析一个脚本(53)
【4月更文挑战第26天】shell脚本解析及训练(53)
15 5
|
1天前
|
弹性计算 运维 监控
每天解析一个脚本(52)
【4月更文挑战第26天】shell脚本解析及训练(52)
18 3
|
1天前
|
运维 监控 Shell
每天解析一个脚本(51)
【4月更文挑战第26天】shell脚本解析及训练(51)
12 4
|
2天前
|
运维 关系型数据库 MySQL
每天解析一个脚本(45)
【4月更文挑战第25天】shell脚本解析及训练(45)
8 0
|
2天前
|
运维 监控 Shell
每天解析一个脚本(44)
【4月更文挑战第25天】shell脚本解析及训练(44)
8 0
|
2天前
|
大数据 图形学 云计算
EDA设计:技术深度解析与实战代码应用
EDA设计:技术深度解析与实战代码应用
|
2天前
|
存储 运维 关系型数据库
每天解析一个脚本(39)
【4月更文挑战第25天】shell脚本解析及训练(39)
7 0

推荐镜像

更多