[difficult and miscellaneous problems of Java] use Java core library to realize simple AOP

Spring is a very popular open source framework, and AOP (aspect oriented programming) is one of the most important concepts of spring. In order to better understand and learn the idea of AOP, it is a good method to use the core library to implement it once.

First, let's introduce the concept of AOP, AOP (aspect oriented programming), that is, aspect oriented programming. The so-called aspect oriented programming is the idea of designing code from a cross-sectional perspective. The traditional OOP idea is to construct a vertical hierarchical relationship with encapsulation inheritance and polymorphism, but it is not suitable to define the horizontal relationship, which is well supplemented by AOP idea.

For example, the log management code is often horizontally scattered in many object levels, but it has nothing to do with the core functions of the corresponding object. There are many similar codes, such as permission verification, debugging output, transaction processing, etc. in this way, it is not conducive to code reuse and management.

At this time, AOP technology came into being. It uses "crosscutting" technology to deeply encapsulate the interior of objects, encapsulates the public behaviors that affect multiple classes into a reusable module, and names it "aspect", that is, aspect. The so-called "aspect" simply means that the logic or responsibilities that have nothing to do with the business but are jointly called by the business modules are encapsulated, which is convenient to reduce the repeated code of the system, reduce the coupling degree between modules, and facilitate the subsequent operability and maintainability.

So how is AOP implemented?

The answer is dynamic proxy (there will be another chapter on proxy, which will not be described in detail here). There are two ways to implement dynamic proxy, one is JDK dynamic proxy and the other is cglib dynamic proxy.

Then use two ways to make a simple chestnut.

First, design a scenario. Suppose we have a computing interface icalculator and a calculator class calculatorimpl that implements the interface.

How to record the total number of times each method of the calculator is used without changing the internal code of the original calculator class?

With a dynamic proxy, it's actually very simple. First create a class and implement the invocationhandler interface to override the invoke method,

Although there seems to be a lot of code, the main method is the invoke method. Object result = in the bind method, pass in the target proxy object and return a proxy class instance. Next, let's look at how to use:

We first define a testhandler, and then obtain a proxy instance through the bind method. Then we can use this instance directly. The operation results are as follows:

In this way, we can extend the code without modifying the internal code of calculatorimpl.

Next, use cglib to implement it once.

First create a class to implement the methodinterceptor interface and override the intercept method. Other codes are similar to using JDK proxy, but the process of obtaining proxy objects is different.

Test:

The operation results are as follows:

Now we have the same result. (you need to import two packages, cglib-2.2.2.jar asm-3.3. Jar)

The two methods have their own advantages. The JDK agent needs to set an interface before it can implement the agent. This is its disadvantage and advantage. The disadvantage is that it will be a little troublesome, and it is impossible to proxy those classes that have been encapsulated and do not implement the interface. The cglib proxy method does not need to use the interface. But because of this, JDK proxy only intercepts the methods that cover the interface in the class, while cglib intercepts all method calls of the class. Both have their own advantages and disadvantages, so they need to be analyzed in detail. In spring, two proxy patterns are also mixed.

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
分享
二维码
< <上一篇
下一篇>>