Interviewer: Talk about your understanding of Spring AOP? Please add these, definitely a plus!

01 Introduction

As we all know, once mentioned AOP, believe and think CGLib JDK proxy agent, yes, these two agents are in we are all conditioned reflexes runtime memory temporarily generated proxy class, and therefore also called enhanced when running - Dynamic Acting . All things are not absolute, since there are dynamic agents, then, is there thought: it is not the existence of a static proxy?

02 LTW(Load Time Weaving)

In fact, in addition to woven into the outer section of the run, there is a way we performed an aspect weaving, it may be by converting byte code of loaded classes , and then woven into a target entry point (target class), this approach is the LTW, i.e., the static agent (also referred to as proxy waiting enhanced when compiling , there will be related later in the code example).

LTW at Java5 when it was introduced, and want to understand the principle, one should first understand a knowledge --Instrument package.

03 works java.lang.instrument package

JDK5.0 introduction of this package, the purpose is to be able to set up access to the underlying JVM. How to access? In fact, speaking personally feel quite troublesome, is required by the JVM startup parameters -javaagent get JVM internal components at start of reference. Parameters format is as follows:

-javaagent:[=options]

Here to sell a related child, in no hurry to explain jarpath parameters and options, run the code sample and the results of the latter will be for the use of the red box labeled instructions, the better.

Well, it has and AOP and do with it?

Because it is fitted and applied ClassTransformer, class bytecode conversion, so as to realize the function starts AOP JVM.

Said the following about two important interfaces instrument under the package:

(1)ClassFileTransformer

It is a Class File Converter interface that there is only one way and, as shown:

image.png

Note: transform method will return a value, type a byte [], byte code represents the converted, but if the return is empty, indicating that no section of the code conversion process, do not as a word of the original class section code empty.

(2)Instrumentation

This interface provides a lot of ways, we mainly pay attention to a method, namely: addTransformer method, its role is to put some ClassFileTransformer registered to the internal JVM, the interface shown in Figure:

image.png

Specific works like this:

After ① ClassFileTransformer instance is registered to the JVM, JVM Class file when loading, it will transform ClassFileTransformer first call () method converts bytecode;

② If the registered more ClassFileTransformer instance, a call is made in the order of registration.

This also to achieve the interception from the JVM byte code level, and then woven into a logical operator you wish to add their own, i.e., the effect achieved AOP.

04 codes and the demonstration effect

Having said that, to point dry, use the following code to give you a demonstration of how to achieve AOP registered to the JVM converter. In order to facilitate reading, important note I have written on the comments of the code or the picture space, pay attention to see.

(1) First, we implement a own converter for analog functions required cut

image.png

Note that, here again stressed, the code return null; not loading bytecode class blanking.

(2) Second, we again achieved a proxy class

image.png

Why should achieve within the proxy class, because it is not a dynamic proxy. . .

(3) Finally, we write a main function, on behalf of program entry

image.png

So far, our Demo be done, first look at the results of running:

image.png

The jar when the local 05 Note

We see the results of the screenshot, when javaagent run cmd parameter specifies an interface myTransformer.jar, this jar is our own need to break out, you can use directly eclipse specific steps as shown below, note illustrated in the figure:

image.png

image.png

06 summary

We can see that, in fact, the use of such agents and there is no dynamic proxy convenient and even switch might have an impact JVM all classes, the operation is relatively cumbersome to update, there will be a lot of inconvenience to the actual production deployment.

But these are written to give you a better, to know more about AOP, AOP fact, we know there are many things to learn to be our own and found, in fact, in the Spring "operational problems" in this regard is still a lot to do things, provides some xml configuration management (here is not to say, because the feeling is a big one that long, we are interested can go and see, write something more about it will not hurt), in many cases under javaagent has no need to configure the parameters.

Finally mention that if those mentioned in the interview, the interviewer believe there will be extra points.


Guess you like

Origin blog.51cto.com/14453419/2424469