In fact, blocking is used like the last example in the groovy Mop 13 interceptor Three (1), which is not used much, but is more about deciding whether to use interceptors in a customer who uses interceptors. Or the example in the previous article:
class Hello {
def hello(name)
{
"hello,$name"
}
}
We are now explicitly intercepting all the methods in the class, and the Interceptor is as follows:
class AllInterceptor implements Interceptor{
Object beforeInvoke(Object object, String methodName, Object[] arguments){
println "the function-$methodName is intercepted"
}
boolean doInvoke(){ true }
Object afterInvoke(Object object, String methodName, Object[] arguments,
Object result){
result
}
}
In the interceptor above, we intercepted all the methods in the intercepted object in the "Beforeinvoke" method and printed out which method was intercepted.
For example, we do the following test code:
def proxy= ProxyMetaClass.getInstance( Hello )
proxy.interceptor= new AllInterceptor()
proxy.use{
def hello = new Hello()
hello.hello('World')
}
The results of the operation are:
the function-ctor is intercepted
the function-hello is intercepted
Indicates that two methods have been intercepted. Now, our clients do not want to intercept the "Hello" method, where we can use block blocking:
def proxy= ProxyMetaClass.getInstance( Hello )
proxy.interceptor= new AllInterceptor()
proxy.use{
def hello = new Hello()
hello.&hello('World')
}
The results of the operation are:
the function-ctor is intercepted
Thus there is no interception of the "Hello" method.