【Groovy】MOP 元对象协议与元编程 ( 方法合成 | 动态注入方法 )

简介: 【Groovy】MOP 元对象协议与元编程 ( 方法合成 | 动态注入方法 )

一、动态注入方法


调用 Student 类不存在的方法 , 如果该类重写了

def methodMissing(String name, def args)

方法 , 就会回调该函数 , 并且可以从参数中拿到方法名和参数列表 ;


在 methodMissing 方法中 , 可以动态注入该不存在的函数 ;


首先 , 获取 org.codehaus.groovy.runtime.HandleMetaClass 类 , 先将 this 赋值给 Student 对象 , 然后通过 Student 对象获取 metaClass ;

// 先将 this 赋值给 Student 对象
        // 然后通过 Student 对象获取 metaClass
        Student student = this
        println student.metaClass


然后 , 根据方法名称 , 动态注入方法 ; 使用 student.metaClass."方法名" = {闭包} 代码进行方法注入 , 注册前 , 不知道方法名称 , 运行时动态确定注入的方法名 ;

println "动态注入 ${name} 方法, 开始注入!"
        // 动态注入方法
        student.metaClass."${name}" = {
            println "执行动态注入 ${name} 方法, 执行相关操作!"
        }
        println "动态注入 ${name} 方法, 注入完毕!"

最后 , 方法注入之后 , 使用 "方法名"(参数列表) 代码调用注入的方法 , 只需要知道方法名就可以调用该方法 ;


// 调用上述动态注入的方法
        // 注意这里传入的参数, 可以直接传入闭包中
        "$name"(args)


二、完整代码示例


完整代码示例 :

class Student {
    def methodMissing(String name, def args) {
        // 直接获取 metaClass
        println metaClass
        // 先将 this 赋值给 Student 对象
        // 然后通过 Student 对象获取 metaClass
        Student student = this
        println student.metaClass
        println "动态注入 ${name} 方法, 开始注入!"
        // 动态注入方法
        student.metaClass."$name" = {
            println "执行动态注入 ${name} 方法, 执行相关操作!"
        }
        println "动态注入 ${name} 方法, 注入完毕!"
        // 调用上述动态注入的方法
        // 注意这里传入的参数, 可以直接传入闭包中
        "$name"(args)
        return null
    }
}
def student = new Student()
// 第一次调用 hello 方法 , 方法没有注入 , 先注入再执行
student.hello()
// 第二次调用hello 方法 , 方法之前注入过了 , 可以直接调用
student.hello()


执行结果 :

第一次调用 :
groovy.lang.MetaClassImpl@3e3047e6[class Student]
org.codehaus.groovy.runtime.HandleMetaClass@3e3047e6[groovy.lang.MetaClassImpl@3e3047e6[class Student]]
动态注入 hello 方法, 开始注入!
动态注入 hello 方法, 注入完毕!
执行动态注入 hello 方法, 执行相关操作!
第二次调用 :
执行动态注入 hello 方法, 执行相关操作!

image.png

目录
相关文章
【Groovy】MOP 元对象协议与元编程 ( 方法合成引入 | 类内部获取 HandleMetaClass )
【Groovy】MOP 元对象协议与元编程 ( 方法合成引入 | 类内部获取 HandleMetaClass )
140 0
【Groovy】MOP 元对象协议与元编程 ( 方法合成引入 | 类内部获取 HandleMetaClass )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Mixin 混合进行方法注入 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Mixin 混合进行方法注入 )
119 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Mixin 混合进行方法注入 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )
128 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Category 分类注入方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Category 分类注入方法 )
130 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Category 分类注入方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 属性缺失 propertyMissing 函数回调 | 方法缺失 methodMissing 函数回调 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 属性缺失 propertyMissing 函数回调 | 方法缺失 methodMissing 函数回调 )
134 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 属性缺失 propertyMissing 函数回调 | 方法缺失 methodMissing 函数回调 )
【Groovy】MOP 元对象协议与元编程 ( Expando 动态类 | 创建动态类 | 为动态类增加字段和方法 )
【Groovy】MOP 元对象协议与元编程 ( Expando 动态类 | 创建动态类 | 为动态类增加字段和方法 )
161 0
【Groovy】MOP 元对象协议与元编程 ( Expando 动态类 | 创建动态类 | 为动态类增加字段和方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
124 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
132 0
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Category 注解进行方法注入 | 分类注入方法查找优先级 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Category 注解进行方法注入 | 分类注入方法查找优先级 )
145 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Category 注解进行方法注入 | 分类注入方法查找优先级 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
180 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )