Java Dynamic agent, and spring AOP

Source: Internet
Author: User

Introduction: Spring AOP (Aspect oriented programming) is implemented through the dynamic agent of Java, for AOP does not know the friends can go online to see the relevant information, I focus on the implementation of the principle is the Java Dynamic Agentto talk about the Java Dynamic Agent will have to say the Java proxy mode, I only give the proxy mode of the UML diagram (1) and dynamic proxy mode UML class diagram (2)

Description: The red-Red Italic class or interface in Figure (2) is provided by the Java class Library that is, proxy and Invocationhandler are Java support for dynamic agents

the correspondence between the figure (1) and the figure (2)

code and explanation of dynamic agent
/** * Abstract Theme **/
/*** an interface that regulates the shape of a dynamically generated proxy class for Realsubject and a proxy provided by Java (the class that implements the Realsubject should look like this)
* This class has a Subjectmethod dynamic agent that does some work before and after the method is executed **/
 Package Dproxy;
Interface Subject { void subjectmethod (int A,int b);}
/** * Specific Topics **/

The/*** implements the subject interface and overrides the Subjectmethod method **/
 Package Dproxy;  Public class Implements Subject {    @Override    publicvoid subjectmethod (intint b) {        //  TODO auto-generated method stub        System.out.println (A + "+" +b + "=" + ( A +b));}    }
/*** Agent processing **/ PackageDproxy;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method; Public classProxyhandlerImplementsInvocationhandler {PrivateObject subject;  PublicProxyhandler (Object subject) {//TODO auto-generated Constructor stub         This. Subject =subject; }/**
* from the figure (2) you can see that Proxyhandler implements the Invocationhandler interface provided by Java for dynamic proxy support, which is represented by an invoke method of the
* function is to add some other functions before and after the Subjectmethod method execution of the specific topic realsubject, because of the function of Proxyhandler, I call it agent processing
* Note that this is not a proxy class, a function delegation class for proxy classes.
**/
@Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {
/**
* We see that invoke has three parameters, where proxy is called by the Java proxy class to invoke the Newproxyinstance method dynamically created by the agent, that is, the proxy class in the dynamic proxy, is it
* in figure (1) It is a class with the same structure as the subject, which is integrated with the subject in the diagram (1), but is dynamically generated in figure (2), although it is dynamically generated, it still inherits the subject interface dynamically.
* and the responsibility to add functionality before and after the Realmethod method is delegated to Proxyhandler.
* But there is a problem, we are not shown here to use
* This proxy, then, what it does. This involves the concept of AOP, the Java language is object-oriented, that is, oop. In OOP there is the concept of encapsulation, inheritance, polymorphism. But one problem is that there are a lot of classes
* There are some common behaviors, such as logs in the Web. The methods for handling the logs are the same, but because of the Java OOP we can only increase the method of processing the log in each class that needs to process the log, which can lead to redundancy of the code.
* So there's AOP, and we can do it in this way for each class log. This method can be used by multiple realsubject, any method of each realsubject can be used, I only show a
*realsubject, and one of the methods in this realsubject. Because it is used by multiple realsubject, multiple dynamically generated proxies are required. Because the invocation of the final Realmethod method is initiated by the dynamic proxy class,
* Then delegate to Proxyhandler, and then call in the Invoke method of Proxyhandler. The second parameter method is a description of the Realmethod method obtained by the reflection mechanism of Java, while args is Realmethod
* parameter list (I only use a Realmthod method here to represent, but also can be other methods of other classes)
**/
//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Processing operation before agent"); System.out.println ("Methods to invoke real-world themes");
/**
* Realmethod method was called with reflection mechanism
**/ method.invoke (subject, args); System.out.println ("Post-agent processing operations"); return NULL; }}
/*** Client **/ PackageDproxy;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy; Public classClient { Public Static voidMain (string[] args) {//TODO auto-generated Method stub
/**
* Create real theme
**/Subject Realsubject =NewRealsubject ();
/**
* Create Agent Processor
**/ Proxyhandler Handler=NewProxyhandler (Realsubject);
/**
* Dynamic creation of proxy objects
* mentioned above the dynamically created proxy object dynamically implements the subject interface.
**/

/**
* Three parameters, the first is the real theme of the ClassLoader, the second is the interface of the real topic, that is, the second parameter to the dynamically generated proxy class has the same interface as subject, the third parameter is the agent processor object
*proxy will delegate the invocation of the Subjectmethod method in the Realsubject object to this handler, which will be processed before and after the call.
**/ Subject Proxy=(Subject) proxy.newproxyinstance (Realsubject.getclass (). getClassLoader (), Realsubject.getclass (). GetI Nterfaces (), handler);
/**
* Dynamically generated Subjectmethod method for invoking dynamically created proxy objects
**/ Proxy.subjectmethod (3, 5); }}

Java Dynamic agent, and spring AOP

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.