SpringAOP basis

Example 1. There are known such a code will be printed out Hello

 public static void main(String[] args) {
        sayHello();
    }
    
    public static void sayHello(){
        System.out.print("Hello");
    }复制代码

Now we want to print out Java Hello world this string, which is output before and after a little before Hello, so I write the following code:

 public static void main(String[] args) {
        System.out.print("Java ");
        sayHello();
        System.out.print(" World");
    }

    public static void sayHello(){
        System.out.print("Hello");
    }复制代码

In this way, our problem has been resolved, but suddenly one day, your boss says, I do not want to print Java, I want to print Python. Then you will be easier ah, change under the code like it, but suddenly, the boss wants to change the C, C ++ ...... has been changed so it would not be appropriate. This time you have to learn under AOP (Aspect Oriented Programming).

First, the basics

1. The concept of off aop not say, starting directly from the code, the process will slowly said. 2.AOP divided into two different types: static and dynamic AOP AOP

Static AOP means: were weaving at compile time, any modification that is to be cut, should recompile the program. AspectJ is a typical example

  • This weaving is a basic concept, it is a white pass code into the execution place. On top of this case is to print more than two in the front and rear Hello, this thought process is weaving.

Dynamic AOP: weaving performed during code execution, the compiled code is not cut it into class files. Spring AOP is a dynamic AOP

Second, the use of AOP technology to solve this case on top

The same code base (just lost it static, this does not affect, are explained in the code)

public void sayHello(){
        System.out.print("Hello");
    }复制代码

Then create a AOP Alliance (Spring AOP Interface) class section of a class that implements the interface


/**
 * 实现环绕通知的接口
 * 环绕通知就是在目标代码前后都会执行的
 */
public class MyInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        //之前
        System.out.print("Java ");

        //这一句就是执行目标代码
        Object proceed = invocation.proceed();

        //之后
        System.out.print(" World");

        return proceed;
    }
}复制代码

Finally, the main operation performed weaving

public static void main(String[] args) {

        //先创建执行目标类方法的对象
        AOPDemo1 target = new AOPDemo1();

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvice(new MyInterceptor());//这个MyInterceptor就是实现SpringAOP接口的类
        proxyFactory.setTarget(target);

        AOPDemo1 proxy = (AOPDemo1) proxyFactory.getProxy();
        proxy.sayHello();
    }

    //因为没有用到类名调用,也不是在main中直接调用,所以不用加静态
    public void sayHello() {
        System.out.print("Hello");
    }复制代码

In this way, the effect is the same as before. I do not understand it does not matter, slowly.

Guess you like

Origin juejin.im/post/5ddd230be51d4532d02d8da2