The article directories

    • What is the AOP
    • The role and advantages of AOP
    • Implementation of AOP
      • A dynamic proxy
        • Dynamic proxy characteristics
        • Dynamic proxies are commonly used in two ways


What is the AOP

AOP: Full name is Aspect Oriented Programming namely: Aspect Oriented Programming.

Aop is a kind of thought, oriented to section programming, is the program for horizontal segmentation, independent extraction of functions, independent implementation. You can avoid mixing a lot of system-specific logic into your business logic code, such as permission management, transaction management, and loggingCopy the code

In a simple way, it is to extract the repeated code of our program, and when it needs to be executed, the dynamic proxy technology is used to enhance our existing methods without modifying the source code.

The role and advantages of AOP

What it does: Enhances existing methods without modifying the source code while the program is running. Advantages: reduce repeated code to improve development efficiency and convenient maintenanceCopy the code

Implementation of AOP

Use dynamic proxy techniquesCopy the code

A dynamic proxy

Dynamic proxy characteristics

Bytecode is created and loaded as you go. This is where it differs from static proxies. Because static proxies are bytecodes created as soon as they are loaded. The decorator pattern is an embodiment of static proxy.Copy the code

Dynamic proxies are commonly used in two ways

Dynamic proxy based on interface

Provider: the JDK's official Proxy class. Requirement: The proxied class implements at least one interface.Copy the code

Subclass-based dynamic proxy

Provider: third-party CGLib. If asMXXXX is abnormal, you need to import ASM. jar. Requirement: A class whose proxied class cannot be modified with final (final class).Copy the code