Explain in detail the implementation of AOP instance using Java Native proxy

When it comes to AOP, people will think of spring, because it is so powerful But we must be clear that AOP is a programming idea, and spring is just an implementation of AOP

First, baidu:

In the software industry, AOP is the abbreviation of aspect oriented programming, which means: aspect oriented programming, a technology to realize the unified maintenance of program functions through precompile and runtime dynamic agent. AOP is the continuation of OOP, a hot spot in software development, an important content in spring framework, and a derivative paradigm of functional programming. AOP can isolate each part of business logic, reduce the coupling between each part of business logic, improve the reusability of program, and improve the efficiency of development.

Today, let's use Java Native proxy to implement simple AOP functions

First, you need to understand the basic knowledge of reflection, otherwise you may be confused

No more wordy, just start rolling code

First, let's write a simple interface The name is animalinterface, which is used to declare some basic methods to regulate animals

These methods include setting names, obtaining names, calls and attributes (forgive me for my lack of culture, but in fact, it is to obtain whether it is terrestrial or aquatic or amphibious)

Then we implement this interface and create a dog named Xiaohei

We can't wait. How to realize functions similar to AOP

Let's first create a class called aophandle to implement the invocationhandler interface. When you can't use proxy as the reflection parameter when invoking, because the interface of the proxy object is different from that of the object. This proxy mechanism is interface oriented rather than class oriented. If you use proxy, infinite recursion will occur Then there is stack overflow, but the reflection can still succeed once, which shows that the proxy object is different from the proxy of the object, but we can use the proxy of the proxy parameter GetClass () obtains the class object, and then obtains the methods and parameters of the proxy class, which also provides an implementation way for annotation injection, specific method injection and attribute injection. Let's talk about this later

Dynamic agent is done Then there is our animalfactory Let's go on

Finally, it's the end... What's the difference? Let's come here to see the effect

Ha ha... Waiter, last dish Oh ~ no, it's a test class Ha ha////

What? What, in the end, it's useless. Isn't it Keng dad? Just catch one of these things. What's the use

What kind of AOP? Why don't I see the shadow of AOP at all? How can I enter a custom method, just a syso input, and fool the audience here

Well, let's continue... To see how to implement injection custom methods

First, add an interface, let's call it AOP injection interface Named aopmethod ha

Create after and before methods to receive object proxy and object [] args parameters

In this way, you can do more things... For example, before executing the method, record the class state and write to log Monitor XX variables,

Open your brain

Then modify the aophandle class and add the aopmethod attribute

Modify the constructor to get aopmethod instance during class initialization

Finally, modify the invoke method Directly on the code ha

Hoo hoo, it's done. Everything seems to be all right, Meng Meng Da

Update the test class quickly

Hoo hoo, folks, do you feel injected? Do you feel like cutting your own method in??? Ha ha

It seems that everything is perfect, but I always feel something bad? Oh, yes, the configuration file like spring is missing

In fact, those are already very simple. Let you do it. Just design the XML format. Wait, what are you talking about? You can't intercept custom methods?

Can't you intercept custom methods like spring? Oh ~ ~ no, actually it's OK. You can make a judgment by using the method and given methodname in before (object proxy, object [] args)

Of course, this example has no practical significance, let alone a variety of perfect AOP frameworks. This article only provides you with an idea, but you must remember that the best things are accumulated little by little

Instance Download: http://xiazai.jb51.net/201701/yuanma/JavaAOP_jb51.rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>