Dynamic agent, annotation, classloader learning

Keywords: Java Junit Spring Attribute

Record learning content
Today's main learning: dynamic agent, annotation, classloader
1, Dynamic agent
Role: enhance a method.
There are three ways to enhance an object:
1. Inherit, override method after inheriting parent class.
Advantages: simple, easy to implement, can inherit the class after rewriting the method.
Disadvantage: you need to know the parent class of the enhanced method.
2. Decorator pattern, using decorator pattern, the decorator and the decorated implement the same interface to enhance the method.
Advantage: you don't need to know the parent class of the object you want to enhance, you just need to know which interface you implement.
Disadvantages: if there are many methods in the interface, all the implementation methods are needed, and the workload is large.
3. Dynamic agent.
Dynamic proxy is to create a proxy class with java's own proxy to enhance the specified method.
A little bit: you don't need to implement all the methods in the interface, just rewrite the methods that need to be enhanced.
Disadvantages: difficult to realize
Code:

package com.topscomm.demo3;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.junit.Test;
/*
 * Details 1. method.invoke() will be executed by anyone calling
 * 
 */

public class demo3 {
    @Test
	public void test() {
		final NomalPerson p =new NomalPerson();
		p.run();
	
		person Proxyperson =  (person)Proxy.newProxyInstance(p.getClass().getClassLoader(), p.getClass().getInterfaces(),
				new InvocationHandler() {
					//Parameter implication
					//The proxy fixed format doesn't matter. The methods passed in by method, that is, the methods that need to be enhanced, args method parameters
					//About return value: if there is a return value in the original method, return will overwrite the return value. If there is no return value in the original method, the return value will not be passed back.
					//Only methods in the interface will be enhanced, and methods not defined in the interface will not be enhanced
					//If you need to filter out methods that do not need to be enhanced, you only need to use the if statement for filtering
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						// TODO Auto-generated method stub
						if("run".equals(method.getName())) {
							method.invoke(p, args);//The original method will be executed here. args is the parameter. If the original method has no parameter, it will not be passed
							System.out.println("It will fly.");
							return null;
						}else {
							return method.invoke(p, args);
						}
						
					}
				});
		Proxyperson.run1();
		
	}
}

Use the decorator pattern to achieve the same case:

public class supurPerson implements person{
	/*
	 * Decorator pattern implementation class method enhancement
	 * (non-Javadoc)
	 * @see com.topscomm.com.demo2.person#run()
	 */
	NomalPerson p = new NomalPerson();
	
	public  supurPerson(NomalPerson p) {
		this.p=p;
	}
	public void run() {
		// TODO Auto-generated method stub
		p.run();
		System.out.println("It will fly.");
	}
}

Case: using dynamic agent to solve get request submitting Chinese code in Servlet

Note: Style: @ with a string
Development only needs to remember: 1. What role does annotation play
2. Annotations can be added there. Where annotations are added is determined by meta annotations, which can be added to variables, methods, and classes.
The annotation underlying implementation is actually an interface. If there is a method in the interface, you need to add the value value value to the method. Annotations are widely used in spring.
Property name of annotation - method name of interface method
Attribute value of annotation - return value of interface method
Meta annotation: @ Target: where can the annotation be placed
@Retention specifies when the annotation is valid

Class loader: class loader is to load the. Class file into memory to form a class object. This process is class loading.
The thing that does this is the classloader.
Composition of class loading gas:
Boot class loader (responsible for loading rt.jar)
Extension class loader (responsible for loading classes other than rt.jar)
Application Loader (loading self written classes)
To ensure that a class can only be loaded once, the "full hosting mechanism" is used
Principle of action:
When the application class loader gets a class, it does not need to do any processing first and hands it to the extension loader
The extension class loader does not do any processing and is handed over to the boot class loader
The bootstrap loader loads its own part and hands it over to the extension loader
After the extension class loader finishes loading its own part, it is handed over to the application class loader‘
Apply class loader to load the rest
'

Published 1 original article, praised 0 and visited 5
Private letter follow

Posted by mizkit73 on Sun, 16 Feb 2020 02:50:18 -0800