Spring's AOP principle

Spring's AOP principle

  Spring's aop is one of the two major features of spring. The cross-cutting modules such as spring shiro and log4J that come with the spring ecosystem have some systematic functions. If these codes are continuously written in the business modules, a large number of repetitive codes may appear. Use aop to do these systematic functions. work, can greatly reduce the amount of code.

  The principle of spring AOP implementation is to use the proxy pattern in the design pattern.


 

  Implementation principle:

  There are two types of proxy modes: static proxy mode and dynamic proxy mode.

      Static proxy: The proxy class and the target class implement the same interface, and the proxy class has a reference to the target class.

  interface

public interface IHello {
    void sayHello(String str);
}

 

target class

public class Hello implements IHello{
    @Override
    public void sayHello(String str) {
        System.out.println("hello:" + str);
    }
}

 

proxy class

public class ProxyHello implements IHello{
    private Hello hello;

    public ProxyHello(Hello hello){
        this.hello = hello;
    }

    @Override
    public void sayHello(String str) {
        Logger.start();
        hello.sayHello(str);
        Logger.end ();
    }

    public static void main(String[] args){
        ProxyHello hello = new ProxyHello(new Hello());
        hello.sayHello( "Zhang Moumou" );
    }
}

 


 

Static proxies have the following problems:

1. If the target class does not implement the interface, how to implement the proxy

2. Do you need to write many proxy classes for different Hellos? It's too troublesome

3. All methods in the interface need to be implemented


 

 

For the above questions:

java proposes to use dynamic proxies to solve these problems

public  class DynamicProxyHello implements InvocationHandler{
     private Object target; // target object

    public Object bind(Object object) {
        this.target = object;
        return Proxy.newProxyInstance(this.target.getClass().getClassLoader(),this.target.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
        Logger.start();
        result = method.invoke(this.target,args);
        Logger.end ();
        return result;
    }

    public static void main(String[] args){
        IHello hello= (IHello) new DynamicProxyHello().bind(new Hello());
        hello.sayHello( "Zhang Moumou" );
    }
}

 

 

It can be seen that a proxy class for any interface can be generated, and enhancement methods are woven into any method, but the disadvantage is that the target class still needs to implement the interface


 

 

The third way:

Use cglib for processing

cglib generates subclasses of the target class, and then rewrites the methods that need to be enhanced. cglib also has limitations, and it cannot handle classes that cannot generate subclasses (final classes).

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325205695&siteId=291194637