Before introducing dynamic proxies, let’s look at what the proxy pattern is. The proxy pattern is used when we do not want to access or cannot access an object directly. The proxy mode generally involves two concepts: delegate class and proxy class. The proxy class is used to handle some transactions for the delegate class, and the proxy class object is often associated with the delegate class object. The proxy mode can be divided into static proxy and dynamic proxy. The static proxy needs to implement a proxy class for each delegate class, and the.class file of the proxy class already exists before the program runs, while the dynamic proxy is dynamically generated at run time using reflection mechanism.

Static agent

To help you understand static agency, let’s take a simple example, Xiaoli wants to buy a lipstick, but the lipstick cannot be bought in China, only available in the United States. At this time, the agent is needed to help Xiaoli to buy lipstick in the United States. Let’s try to do that.

First we define a Subject interface as follows, which mainly declares what the real object wants the proxy object to do.

public interface Subject {

 void buy(a);

}

Copy the code

Then create the delegate class.

public class RealSubject implements Subject {



 @Override

 public void buy(a) {

  System.out.println("I want to buy some lipstick.");

 }

}

Copy the code

Then create the proxy class, which we call a daigou.

public class StaticProxy implements Subject {

 

 private Subject subject;



 public StaticProxy(Subject subject) {

  this.subject = subject;

 }



 @Override

 public void buy(a) {

  subject.buy();

 }



}

Copy the code

Let’s create a test class to test the code.

public class TestProxy {



 public static void main(String[] args) {

  Subject subject = new StaticProxy(new RealSubject());

  subject.buy();

 }

}

Copy the code

The code above demonstrates the StaticProxy process. You can see that we implement the Subject interface using the proxy class StaticProxy. Now let’s look at how dynamic proxy is implemented.

A dynamic proxy

Let’s recreate a dynamic proxy class that implements the InvocationHandler interface.

public class DynamicProxy implements InvocationHandler {

 

 private Object object;

 

 public DynamicProxy(Object object) {

  this.object = object;

 }



 @Override

 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

  // Call a method on a delegate object

  Object result = method.invoke(object, args);

  return result;

 }

}

Copy the code

The code above shows the basic implementation idea of a dynamic proxy, defining an Object as an object of a delegate class, and actually executing a method is done in the invoke() method. At this point we don’t have to worry about who the object of the proxy is, we just need to implement a set of logic. Let’s modify the test class.

public class TestProxy {



 public static void main(String[] args) {

  Subject subject = new RealSubject();

  Subject dynamicProxy = (Subject) Proxy.newProxyInstance(

    subject.getClass().getClassLoader(), 

    subject.getClass().getInterfaces(), 

    new DynamicProxy(subject));

  dynamicProxy.buy();

 }

}

Copy the code

Dynamic proxy works the same as static proxy, and dynamic proxy saves us the trouble of writing proxy classes repeatedly.

Spring AOP can use both JDK dynamic proxies and CGLIB dynamic proxies. By default, JDK dynamic proxies are used. If an object does not implement an interface, The CGLIB proxy is used. One of the main advantages of CGLIB over JDK dynamic proxies is that the class being propped by the JDK dynamic proxy cannot be a normal class, it must implement an interface. CGLIB, on the other hand, is a dynamically generated subclass of the proxy class, in which method interception intercepts calls to methods of the parent class. This requires that the proxied class cannot be modified by final, because final modified classes cannot be inherited.

AOP

Those of you who have studied Spring have certainly heard of AOP, but AOP did not come from Spring, but Spring is a typical application of AOP. AOP full name is Aspect Oriented programming, Aspect oriented programming. In traditional object-oriented programming, there are some crosscutting problems in business logic. The idea of AOP is to decouple core code from these crosscutting problems and improve the reuse rate of code.

About AOP, foreigners give take a lot of terms, similar to Ali “grab, empower, precipitation” that set of disgusting Internet talk, we understand it.

Noun explanation

  • Join point. A point in program execution, such as method execution or exception handling. In Spring AOP, join points always represent method execution.

  • “Pointcut”. Instead of all methods being join points, a pointcut can be a specific method. By default, Spring uses AspectJ pointcuts to express the language.

  • “Advice”. The actions taken at a particular join point. Different types of Advice include things like before()/after().

  • Aspect. Consists of Pointcut and Advice.

  • Weaving. The process of creating a proxy object by applying Advice to a target class.

  • “Introduction.” Allows you to declare new methods and fields to existing classes.

Common Application Scenarios

  • Spring declarative transaction management configuration.

  • Parameter verification of the Controller layer.

  • Check login permission.

  • Log printing.