Explore groovy mop nine interceptor 1

Source: Internet
Author: User

Explore groovy mop nine interceptor 1

 

 

In recent years, AOP (Aspect-Oriented Programming) has been widely used. We have applied it to various aspects such as printing logs and permission control. When Implementing AOP, we generally use tools such as Spring AOP.

Of course, tools are generally used to implement system-level AOP. Such implementations are generally implemented through configuration documents.

When we need to implement a small range of AOP, such as performing AOP on some methods of a limited number of classes, we generally do not want to use tools. The reason may be that the first tool is not flexible enough, and the second requires tedious configuration. At this time, we need to implement all aspects of AOP by ourselves.

Java generally has two methods to implement their own AOP technology: One is through reflection technology, and the other is through dynamic proxy. These two methods are not mentioned here, but they are complicated to implement.

In groovy, we can use Interceptor to implement the AOP technology. Using Interceptor to implement the AOP technology is much less technical and more flexible.

This series of texts is about all aspects of Interceptor Technology. from how it intercepts a method, we have been talking about how to use it to flexibly and flexibly use our own AOP.

Let's start with a simple example. For example, we have the following class:

 

ClassHelloworld {

DefHello ()

{

Println'Hello'

}

 

}

 

Now we want to intercept its "hello" method.

First, we need to implement our own interceptor class. In groovy programming, it is very easy to implement our own interceptor class, that is, to implement the interceptor interface, this interface only has three simple implementation methods. As follows:

 

ClassSampleinterceptorImplementsInterceptor {

Object beforeinvoke (Object object, string methodname, object [] arguments)

{

If(Methodname = 'hello ')

{

Println'Before invoke hello'

}

}

BooleanDoinvoke (){True}

Object afterinvoke (Object object, string methodname, object [] arguments,

Object result ){

Result

}

 

}

 

From the instance code above, we can see that our interceptor class needs to implement three methods, namely the "beforeinvoke" method, used for the action before calling the method; "doinvoke" method, determines whether to call the method to be intercepted. The "afterinvoke" method is used for the action after the method is called.

In the above instance code, we intercepted the "hello" method of the helloworld class and printed the words "before invoke hello" before calling this method.

Now, we can test our first interceptor class:

 

DefProxy = proxymetaclass. getinstance (helloworld)

Proxy. interceptor =NewSampleinterceptor ()

Proxy. Use {

DefHelloworld =NewHelloworld ()

Helloworld. Hello ()

}

 

The Interceptor is also very simple to use. The first is to instantiate a "proxymetaclass" class instance "proxy", and then assign the instance of our interceptor class to "Interceptor" of the "proxy" object ". Finally, the class to be intercepted is instantiated in the "use" method of the "proxy" object and its method is called.

The result of the above Code is:

Before invoke hello

Hello

 

This completes a simple interception task.

After we first understand our own simple interceptor, We need to thoroughly understand all aspects of the interceptor.

First, we can use the Interceptor to intercept a method of a class and actually call this method. Just like in the above example, we can actually not call this method. As follows:

 

ClassSampleinterceptorImplementsInterceptor {

Object beforeinvoke (Object object, string methodname, object [] arguments)

{

If(Methodname = 'hello ')

{

Println'Before invoke hello'

}

Object

}

BooleanDoinvoke (){False}

 

Object afterinvoke (Object object, string methodname, object [] arguments,

Object result ){

Result

}

 

}

 

Or the interceptor class above. In order not to call the method we want to intercept, we made some changes. First, the return value of method "doinvoke" is "false", which determines not to call the method to be blocked. If you do not call the method to be blocked, the "beforeinvoke" method must have a return value. The code above returns "object ". Otherwise, a null pointer violation is reported.

Now, we still use the code used above to test it:

 

DefProxy = proxymetaclass. getinstance (helloworld)

Proxy. interceptor =NewSampleinterceptor ()

Proxy. Use {

DefHelloworld =NewHelloworld ()

Helloworld. Hello ()

}

The running result is:

Before invoke hello

 

It is worth noting that in the above example, we all use the "beforeinvoke" method as an example, which is used for action before calling the intercepted method, if you want to perform an action after calling the intercepted method, you need to use "afterinvoke". The usage of both methods is similar.

Now let's take a look at the parameters of the "beforeinvoke" and "afterinvoke" methods. "Object" is the intercepted object, "methodname" is the intercepted method name, and "arguments" is the parameter of the intercepted method. The following is a test.

First, we made some changes to the "helloworld" class to facilitate our testing:

 

ClassHelloworld {

 

DefHello (name)

{

Println"Hello, $ name! "

}

 

}

 

Similarly, we also need to change our interceptor class:

 

ClassSampleinterceptorImplementsInterceptor {

Object beforeinvoke (Object object, string methodname, object [] arguments)

{

If(Methodname = 'hello ')

{

Println"Object: $ object"

Println"Method name: $ methodname"

Println"Method arguments: $ arguments"

}

}

BooleanDoinvoke (){True} // Whether or not to invoke the intercepted

 

Object afterinvoke (Object object, string methodname, object [] arguments,

Object result ){

Result

}

 

}

 

Finally, let's test the above changes:

 

DefProxy = proxymetaclass. getinstance (helloworld)

Proxy. interceptor =NewSampleinterceptor ()

Proxy. Use {

DefHelloworld =NewHelloworld ()

Helloworld. Hello ('World ')

}

 

Result:

Object: mop. Interceptor. Basic. helloworld @ f0eed6

Method Name: Hello

Method arguments: {"world "}

Hello, world!

 

Finally, let's take a look at the "result" parameter of the "afterinvoke" method, which is the return value of the intercepted method. In the above example, we directly return this parameter, indicates that the return value of the intercepted method is not changed. Speaking of this, we thought of it. Of course, we can change the return value of the blocked method. As follows:

 

ClassHelloworld {

 

Def Test()

{

'Hello'

}

 

}

 

Let's modify the interceptor class:

 

ClassSampleinterceptorImplementsInterceptor {

Object beforeinvoke (Object object, string methodname, object [] arguments)

{

}

BooleanDoinvoke (){True}

 

Object afterinvoke (Object object, string methodname, object [] arguments,

Object result ){

If(Methodname = 'test ')

{

PrintlnResult

Result = 'World'

}

Result

}

 

}

 

Test:

DefProxy = proxymetaclass. getinstance (helloworld)

Proxy. interceptor =NewSampleinterceptor ()

Proxy. Use {

DefHelloworld =NewHelloworld ()

DefResult = helloworld.Test()

Println'After interceptor, The result: '+ Result

}

 

Result:

Hello

After interceptor, The result: World

 

 

Related Article

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.