Groovy exploration of the mop nine interceptor A

Source: Internet
Author: User
Tags aop object object

In recent years, AOP (aspect-oriented programming) has been widely used, we apply it to such as print log, permission control and so on. When it comes to AOP, we typically use tools such as spring AOP, and so on.

Of course, tools are commonly used to implement system-level AOP, which is typically implemented with the help of configuration documents.

When we need to implement a smaller range of AOP, such as AOP for certain methods of a finite number of classes, we generally do not want to use tools. The reason may be that the first tool is not flexible. The second need to do cumbersome configuration work. At this point, we need to implement all aspects of AOP ourselves.

Java language in general, there are two ways to implement your own AOP technology: one is through reflection technology, and the other is using dynamic proxies. These two methods are not much said here, but the implementation is more complex.

In the groovy language, we can implement AOP technology through interceptor (interceptors). Using interceptor to implement AOP technology is much less difficult and more flexible.

The text of this series is about all aspects of interceptor technology, from how it intercepts a method, and how it is used to flexibly use our own AOP.

Let's talk about it from a simple example. For example, we have one of the following classes:


class HelloWorld {

def hello()
{
println 'hello'
}
}

We are now going to intercept its "hello" method.

First, we want to implement our own interceptor class, in the groovy language programming, the implementation of their own interceptor class is very simple, is to implement the Interceptor interface, and this interface only a simple three need to implement the method. As shown below:

class SampleInterceptor implements Interceptor{

Object beforeInvoke(Object object, String methodName, Object[] arguments)
{
if(methodName == 'hello')
{
println 'before invoke hello'
}
}
boolean doInvoke(){ true }
Object afterInvoke(Object object, String methodName, Object[] arguments,
Object result){
result
}

}

As you can see from the above example code, our interceptor class needs to implement three methods, the "Beforeinvoke" method, which is used to do the action before the method is invoked, and the "Doinvoke" method to decide whether to call the method that needs to be intercepted; "Afterinvoke" method, The action to do after the calling method.

In the example code above, we intercepted the "hello" method of the HelloWorld class and printed the words "before invoke Hello" before calling the method.

Now we can test our first Interceptor class:

def proxy= ProxyMetaClass.getInstance( HelloWorld )

proxy.interceptor= new SampleInterceptor()
proxy.use{
def helloworld= new HelloWorld()
helloworld.hello()
}

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.