小编Aba*_*rdi的帖子

Groovy方法拦截

在我的Grails应用程序中,我安装了Quartz插件.我想拦截对每个Quartz作业类方法的调用execute,以便在execute调用方法之前做一些事情(类似于建议之前的AOP).

目前,我正试图通过doWithDynamicMethods关闭另一个插件来执行此拦截,如下所示:

def doWithDynamicMethods = { ctx ->
    // get all the job classes
    application.getArtefacts("Job").each { klass ->

        MetaClass jobMetaClass = klass.clazz.metaClass

        // intercept the methods of the job classes
        jobMetaClass.invokeMethod = { String name, Object args ->

            // do something before invoking the called method
            if (name == "execute") {
                println "this should happen before execute()"
            }

            // now call the method that was originally invoked
            def validMethod = jobMetaClass.getMetaMethod(name, args)

            if (validMethod …
Run Code Online (Sandbox Code Playgroud)

grails groovy metaprogramming

8
推荐指数
1
解决办法
1346
查看次数

标签 统计

grails ×1

groovy ×1

metaprogramming ×1