Interviewer: Tell us about your understanding of SpringAOP of? Please add the content, absolute bonus!

Author: Java architecture Pride and Prejudice Original Address: www.toutiao.com/a6695345484485100044

I. 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?

二、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.

Three, java.lang.instrument package works

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 startup parameters ** JVM - javaagent ** get JVM internal components at startup 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:

  • ClassFileTransformer

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

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.

  • 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:

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.

Fourth, the code 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.

  • First, we implement a own converter for analog functions required cut

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

  • Secondly, we again achieved a proxy class

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

  • Finally, we write a main function, on behalf of program entry

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

Five, playing jar when caveats

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:

VI 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.

Top Content:

1, the system has a Bug? These Tuning Guide to help you solve most problems!

2, 200,000 simultaneous users to access a hotspot Key, how to optimize the cache architecture?

3, multi-threaded, in the end how many threads set?

4, the production environment in the end how to deploy Tomcat?

5, God appreciation class code comments, likes to take it with!

6, open source 13 Spring Boot excellent learning project! 53K Super Star, a clean sweep!

7, Huawei Hass, president letter to employees: no longer have the opportunity to change the tire, when the technology will be self-!

8, do not understand this 12 syntactic sugar, not to mention you'll Java!

[Video] welfare 2T free learning videos, search or scan the two-dimensional code micro-channel public concern number: Java back-end technology (ID: JavaITWork), and 20 million people with learning Java! Re: 1024 to access our FREE! Contains SSM, Spring family bucket, micro-services, MySQL, MyCat, clusters, distributed, middleware, Linux, network, multi-threaded, Jenkins, Nexus, Docker, ELK and so free learning videos, continuously updated!

Reproduced in: https: //juejin.im/post/5cf34443f265da1bb77651d4

Guess you like

Origin blog.csdn.net/weixin_34056162/article/details/91430613