Spring Series __04AOP

About AOP

Today to introduce AOP. AOP, Chinese design method is often translated as "Aspect Oriented Programming", as OOP extension, the idea has been applied in addition to Spring, it is good. Typically, a software system, in addition to the normal business logic code, there are often some of the functionality of the code, such as: logging, data verification and the like. The most primitive way is to write the functional code directly in your business logic code, however, this was in addition to the development of more convenient time; code readability, maintainability will be greatly reduced. And, when you frequently need to use a function of time (such as logging), you also need to repeat writing. The benefits of using AOP, in short, is that it can function code that repetitive pulled out, when needed, by dynamic agent technology, provide enhanced sexual function without modifying the source code.
Advantage:

  • Reduce duplication of code
  • Improve development efficiency
  • Code more clean and improve the maintainability
    said so much, simply show you, we assume that there is a calculator objects, and to achieve basic arithmetic operations; the same time, we hope that the appropriate logging operations.
public interface ArithmeticCalculator {

    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);

}

Here, we define an interface ArithmeticCalculator to complete the addition and subtraction multiplication and division, and to entrust its implementation class to complete its implementation.

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    @Override
    public int add(int i, int j) {
        System.out.println(i + "和 " + j + "开始加法运算");
        int reusult = i + j;
        System.out.println("加法运算结果是: " + reusult);
        return reusult;
    }

    @Override
    public int sub(int i, int j) {
        System.out.println(i + "和 " + j + "开始减法运算");
        int reusult = i - j;
        System.out.println("减法运算结果是: " + reusult);
        return reusult;
    }

    @Override
    public int mul(int i, int j) {
        System.out.println(i + "和 " + j + "开始乘法运算");
        int reusult = i * j;
        System.out.println("乘法运算结果是: " + reusult);
        return reusult;
    }

    @Override
    public int div(int i, int j) {
        if (j == 0) {
            throw new ArithmeticException("除数" + j + "不能为0");
        }
        System.out.println(i + "和 " + j + "开始除法运算");
        int reusult = i / j;
        System.out.println("除法运算结果是: " + reusult);
        return reusult;
    }
}

Here, we see a very sick Code: logging a lot of repetitive code, but when you change, you do not find convenient. We will follow this code rewrite.

AOP implementations

AOP is achieved by dynamic proxy.
Here briefly explain the dynamic agent: Use a proxy object packing up, and then replace the original object with the proxy object .. Any call to the original object must be through a proxy. Proxy object decide whether and when to call the method on the original object. Its calling procedure is shown below:

Guess you like

Origin www.cnblogs.com/JackHou/p/11530932.html