AOP in Java

Source: Internet
Author: User

Just beginning to touch Java, always hear AOP technology is the aspect-oriented programming technology, has been very confused, what it is, what to do with, the implementation of the principle of what. Make a summary here today.

Let's start by introducing the origins of AOP technology:

In our business systems, it is sometimes necessary to use "some code" in the business system to perform some common actions, such as writing logs, database connection management, and transaction management. Then we need to add duplicate code in many ways, which makes the code repetition rate too high and not good for maintenance. So the idea is to ask each action to inherit the framework to provide a good baseaction, and then implement the Excute method. However, the lack of flexibility in this way is that every action we need to write a class to complete, for the complex system, we need to write too many such classes, so we expect a dynamic way to proxy operation, so the dynamic agent mechanism appears, AOP technology is actually bytecode enhancement technology, The dynamic proxy traced provided by the JVM is also byte-code enhancement technology.

The implementation principle of bytecode enhancement technology:

Bytecode enhancement techniques are implemented in two ways:

One is to inherit the original class by creating a subclass of the original class, that is, a new class that is dynamically created.

Two is a very violent way to modify the class byte code directly.

Steps to implement bytecode enhancement techniques:

One is to get the original bytecode in memory, modify its byte[] array with some open source APIs, and get a new byte[].

Second, write this new byte[] array to the PermGen area, which is to load the new byte[] and replace the original class byte code.

The implementation principle and introduction are introduced here, here we have an example.

We use the Open source API to implement bytecode enhancement technology for the Javaassit API.

We construct a transformer method, which needs to implement the interface Classfiletransformer and implement the Transform method, the code is as follows:

1. Modify Class byte code

 Public classTesttransformerImplementsClassfiletransformer {@Override Public byte[] Transform (ClassLoader loader, String className, class<?>classbeingredefined, Protectiondomain Protectiondomain,byte[] classfilebuffer)throwsillegalclassformatexception {System.out.println ("Load class:" +className); //Judging whether it is necessary for us to cut the target class        if("/targetclass". Equals (ClassName)) {Ctclass Ctclass=classpool.getdefault (). Get (Classname.replace ('/', '. '))); //get the target method that needs to be cutCtmethod Ctmethod=ctclass.getdeclaredmethod ("Display1"); //Insert a method that needs to be executed beforeCtmethod.insertbefore ("Name=\" xxxx\ ";"); //Insert a method that needs to be executed after Display1Ctmethod.insertafter ("System.out.println (value);"); returnCtclass.tobytecode (); }        //TODO auto-generated Method Stub        return NULL; }}

2, register in the instrumentation, load the new class byte code

   Public class instfortransformer{   publicstaticvoid  premain (String Agentargs, Instrumentation instp) {     Instp.addtransformer (new  Testtransformer ());   }  

If the registration succeeds, then the user class is loaded in the JVM, and the corresponding Testtransformer class's Transform method is called to obtain the modified bytecode, which is the purpose of modifying the bytecode. Find a lot of Java Tutorials Do not explain this, let me make up for it

AOP in Java

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.