14. analyze the role and principles of proxy classes and the concept of AOP

Source: Internet
Author: User

Analyze the role and principles of proxy classes and the concept of AOP

1. Concepts and functions of proxy

· Agents in life

Buy Books --------- bookstore ----------- publishers

· Agents in the program

How do you prepare to add some system functions, such as processing, logs, running time of calculation methods, and transaction management, to the methods of Multiple Target classes with the same interfaces?

· Compile a proxy class with the same interface as the target class. Each method of the proxy class calls the same method of the target class, and add the system function code when calling the method.

· If the factory mode and configuration file are used for management, you do not need to modify the client program. In the configuration file, configure the target class or the proxy class, which is easy to change in the future, for example, if you want to configure the agent class when logging, or configure the target class, it is easy to add the system function. It is also easy to remove the system function after running for a period of time.

2. AOP (Aspect-Oriented Programming)

There is a cross-business in the system. A cross-business is to be integrated into the system as follows:

Security, transactions, logs, and other functions need to run through many modules. Therefore, they are cross-business. We are not targeting a class, not an object, but a specific aspect, what we need to do is to modularize these sections and write only one copy.

Here, we will use the proxy to run the Method1 method of the student class using the proxy class of the student class, and execute the required methods such as security, transactions, logs, and so on during the runtime, the running effect is the same.

3. Dynamic proxy technology

(1) adding proxy functions for the classes of various interfaces in the system will require too many proxy classes, all using static proxy, which will be very troublesome, isn't it tiring to write hundreds of proxy classes.

(2) The JVM can dynamically generate class bytecode during runtime. Such dynamically generated classes are often used as proxy classes, that is, dynamic proxies.

(3) the dynamic class generated by JVM must implement one or more interfaces. Therefore, the dynamic class generated by JVM can only be used as a proxy for the target class with the same interface.

(4) The cglib library can dynamically generate a subclass of a class, and a subclass of a class can also be used as a proxy for this class. Therefore, if you want to generate a dynamic proxy class for a class without an implemented interface, you can use the cglib library.

(5) In each method of the proxy class, in addition to calling the corresponding method of the target and returning the results of the target, you can also add the system function code in the following four locations of the proxy method: between the call targets, after, before, and after the exception catch

4. Create a dynamic class and view its method list

Display:

5. Create a dynamic class instance object and call its Method

The remaining four sets are not displayed, which is a bit difficult. Transfer to ibatis !!!

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.