Black Horse programmer agent Learning

Source: Internet
Author: User

----------- Android training, Java training, and hope to communicate with you! ------------

Proxy

In our daily life, there are agents between consumers and manufacturers. Agents provide certain functions, such as delivering goods to consumers and keeping them fresh.

There are similar middleware-proxy classes in the program.

Proxy class: it has the same interface (method set) as the target class. Each method in the proxy calls the method of the target class and provides some additional functions, such as the running time of the calculation method, exception Handling, transaction management, logs, etc.

Specific applications: if the factory mode and configuration file are used for management, you can easily switch between the target class and the proxy class in the configuration file. At the same time, if you want to remove the additional features in the future, it is also very convenient.

 

Sample Code

Class A {// target class void method () {}} class xproxy {// proxy class void method () {// starttime has the same method as the target class; // adds the system function.. method (); // call the method endtime of the target class; // Add system function }}

Proxy architecture diagram:

 

 

 

AOP

Proxy is the core and key technology for implementing the AOP function.

So what is AOP?

A: Aspect Oriented Program: Aspect-Oriented Programming.

 

What is cross-business?

A: security, transactions, logs, and other functions need to run through many modules, which are cross-business.

 

The goal of AOP is to modularize cross-business.

How to implement it?

A: You can move the aspect code around the original method. This is the same as writing the aspect Code directly in the method. (The aspect code here is the additional function code)

In actual development, the Section code is encapsulated into an object and passed to the invocationhandler's invoke () method.

 

 

 

Dynamic proxy technology

To add proxy functions for the classes of various interfaces in the system, many proxy classes are required if static proxy is used.

 

Dynamic proxy class: JVM can dynamically generate class bytecode during runtime. This dynamically generated class is often used as a proxy class, that is, dynamic proxy class.

The dynamic class generated by JVM must implement one or more interfaces. Therefore, it can only represent the target class with the same interface.

 

Cglib Library: You can dynamically generate a subclass of A Class. A subclass of a class can also be used as a proxy of this class. Therefore, you can use the cglib library to generate a proxy for a class without an interface. (A plug-in)

 

The system function code (aspect Code) is stored in the proxy method. Location: 1. before calling the target method; 2. After calling the target method; 3. The Catch Block for exception handling in the proxy method.

 

JVM dynamically generates class bytecode: instance code

Dynamically generate a class bytecode that implements the collection interface, and use reflection technology to view some internal information of this dynamic class.

Import Java. util. *; import Java. lang. reflect. *; Class proxydemo {public static void main (string [] ARGs) {class clazzproxy = proxy. getproxyclass (collection. class. getclassloader (), collection. class); // dynamically generate a class system. out. println (clazzproxy. getname (); system. out. println ("---------- Construction Method: -------"); constructor [] constructors = clazzproxy. getconstructors (); For (constructor: constructors) {system. out. println ("constructor:" + constructor);} system. out. println ("---------- normal method: -------"); method [] Methods = clazzproxy. getmethods (); For (method: Methods) {system. out. println ("common method:" + method); Class [] clazzparams = method. getparametertypes (); For (class clazzparam: clazzparams) {system. out. println ("---- parameter:" + clazzparam. getname ();} type [] types = method. getgenericexceptiontypes (); For (type: types) {system. out. println ("-------- the exception type thrown by the method is:" + type );}}}}

Create an instance object for the dynamic proxy class as follows:

Collection proxy3 = (Collection) proxy. newproxyinstance (collection. class. getclassloader (), // The first parameter: new class [] {collection used to generate dynamic class interfaces. class}, // second parameter: array of types used to generate dynamic class interfaces new invocationhandler () {// third parameter: implement the invocation interface's Anonymous class Object arraylist target = new arraylist (); // target Class Object public object invoke (Object proxy, method, object [] ARGs) throws throwable {// method call function of the dynamic proxy class long starttime = system. currenttimemillis (); object retval = method. invoke (target, argS); // call the function long endtime = system. currenttimemillis (); system. out. println (method. getname () + "method running time" + (endtime-starttime); Return retval ;}}); proxy3.add ("hello"); proxy3.add ("Java"); system. out. println ("number of set elements:" + proxy3.size ());}

Invocationhandler object running principle:

When the client calls the proxyobj. Add ("AB") method, three elements are involved: 1. Which object is called? proxyobj 2. which method is called? 3. The parameters of the called method.

Call process: Use proxymethod. invoke (), This method requires the return value of the target method, it will automatically call targetmethod. invoke (), and then pass the return value to proxymethod. invoke (), and then pass it to the caller. During the process of passing the value, additional code of the proxy class also runs successively.

 

Method for Analyzing proxy classes:

class Proxy$ {add(Object obj) {return handler.invoke(Object proxy, Method method, Object[] args)}int size() {return handler.invoke(this, this.getClass().getMethod("size"), null);}}

 

Working principle of dynamic Proxy:

 

----------------------- Android training, Java training, and hope to communicate with you! ----------------------

For details, see:Http://edu.csdn.net/heima

 

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.