Groovy Exploration of Mop 13 Interceptor III (2)

Source: Internet
Author: User
Tags object object

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.