Generic method interception in grails (specifically Controllers)

。_饼干妹妹 提交于 2020-01-14 01:37:06

问题


I'm trying to create a generic function in grails that will allow me to specify a class and function name, and intercept any function calls based on that criteria:

getSomeClass().metaClass.invokeMethod = { String methodName, args ->
    MetaMethod someAction =  getSomeClass().metaClass.getMetaMethod(methodName, args)
    def result = someAction.invoke(delegate, args)
    if (methodName==getSomeMethodName())
        intercept(args, result)
    return result
}

This works for POGO, and domain classes, but does not seem to work for controller classes. While I'm fully aware there are Controller interceptors and filters available in Grails, these don't really centralise what I'm trying to achieve, and was trying to create a simple generic function for some centralised behaviour

Any guidance on why this doesn't work on Controllers would be appreciated, thanks


回答1:


Your approach will work for method calls that are made through the Groovy metaclass mechanism, but in Grails 2 this doesn't apply to controller actions - they're called using normal Java reflection (java.lang.reflect.Method.invoke), and therefore your custom invokeMethod is bypassed.

If you want an AOP mechanism that'll work for calls from Java as well as from Groovy you'll probably have to use something like AspectJ load-time weaving. Spring's proxy-based AOP may work but the Grails 2 controller system relies on the action methods having a particular @Action annotation (which is added at compile time by an AST transformation) and I don't know whether Spring AOP proxies preserve method annotations from the target class on the generated proxy methods.




回答2:


Could it be that MyController.metaClass.invokeMethod is overwritten by the grails framework after your definition?

Have you tried to check the content of MyController.metaClass.invokeMethod through reflection?



来源:https://stackoverflow.com/questions/10956841/generic-method-interception-in-grails-specifically-controllers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!