I don’t have enough time to read these two sentences
JDK dynamic proxy interfaces exist, but cglib dynamic proxy interfaces do not exist.
Aop when loading all the cutting class, get the cutting point and the method of this class one by one comparison, matching the implementation of dynamic proxy.
Dynamic proxy
AOP in the most core point is dynamic proxy, dynamic proxy is to help us extend existing functions, so how to achieve dynamic proxy?
JDK dynamic proxy
JDK dynamic proxies are an approach to proxy implementation that is appropriate for using interfaces.
The main steps are: create interfaces, generate interface implementation classes, create classes for proxy logic, and test.
(1) Create interfaces
public interface HelloWorld {
public void sayHello(a);
}
Copy the code
(2) Create the interface implementation class
public class HelloWorldImpl implements HelloWorld{
public void sayHello(a) {
System.out.println("Hello World"); }}Copy the code
(3) Create a class of proxy logic and implement the Invoke method, which writes the proxy logic, that is, extends the original method.
public class JdkProxyExample implements InvocationHandler {
// Create a real object
private Object target = null;
// Establish a relationship between the proxy object and the real object
public Object bind(Object target){
this.target = target;
return Proxy.newProxyInstance
(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
// Proxy method logic
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Enter agent logic");
System.out.println("Services before scheduling real objects");
Object obj = method.invoke(target, args);
System.out.println("Services after scheduling real objects");
returnobj; }}Copy the code
The test.
public class testJdkProxy {
public static void main(String[] args) {
JdkProxyExample jdk = new JdkProxyExample();
HelloWorld proxy = (HelloWorld) jdk.bind(newHelloWorldImpl()); proxy.sayHello(); }}Copy the code
2. Cglib dynamic proxy
There are actually classes that don’t have interfaces. How do these implement dynamic proxies? This is where the Cglib dynamic proxy comes in.
Enhanced classes are used here to implement dynamic proxies, generate proxy objects, and then manipulate proxy logic.
public class CglibProxyExample implements MethodInterceptor {
// Generate a proxy object
public Object getProxy(Class cls){
// Enhance class objects
Enhancer enhancer = new Enhancer();
// Set the enhancement type
enhancer.setSuperclass(cls);
// Define the proxy logical object as the current object
enhancer.setCallback(this);
// Generate and return the proxy object
return enhancer.create();
}
// Proxy logic method
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("Before calling the real object");
Object result = methodProxy.invokeSuper(o,objects);
System.out.println("After calling the real object");
returnresult; }}Copy the code
The test.
public class testCglibProxy {
public static void main(String[] args) {
CglibProxyExample cpe = newCglibProxyExample(); HelloWorldImpl obj = (HelloWorldImpl) cpe.getProxy(HelloWorldImpl.class); obj.sayHello(); }}Copy the code
Second, the AOP
Aop operations essentially use dynamic proxies to extend existing classes and methods.
First we define an Aspect by adding an @Aspect to a class, and of course we add an @Component annotation to the class to add that class to the container.
When A class A needs to be cut horizontally using AOP, Spring will see if the class’s parent has an interface, that is, if it has an implementation class for that interface. If so, Spring will choose JDK dynamic proxies, and if not, cglib dynamic proxies.
A To create an object, the process is as follows:
Class — > Instantiate objects — >DI injection — > Initialize — > AOP (proxy object) — > Beans
When you get to AOP, Spring takes all the @Aspect classes, finds their pointcuts and compares them to the methods in the current class. Pointcuts are easy to locate because of the expression, and if they are found, they are executed.
To create a proxy object, the proxy object either way takes the target object and calls the target method in the proxy class with proxy logic around it.