There is still a lot of knowledge involved in Dubbo, so I will not enter the source code first, but first take a look and sort out the knowledge involved. Clear out the peripheral positions first, before entering the core position!
Peripheral:
1: JDK dynamic proxy:
This can refer to the website: http://blog.jobbole.com/104433/. (Thank you to the author: @博乐在线 - looking for "four-leaf clover") It is written in great detail and also comes with an analysis of the JDK source code.
2: Javassist (Java bytecode generation open source framework):
import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; import javassist.CtNewMethod; public class MyJavassist { public static void main(String[] args) throws Exception { ClassPool pool = ClassPool.getDefault(); //Create Test class CtClass cc= pool.makeClass("com.jishuaige.Test"); //Define the test method CtMethod method = CtNewMethod.make("public void test(){}", cc); // insert method code method.insertBefore("System.out.println(\"test.....\");"); cc.addMethod(method); cc.writeFile("d://myJavassist"); } }
3: Java spi mechanism (emphasis):
The mechanism provided by Java's SPI (Service Provider Interface): We define the interface standard of the service, and let each implementer complete their own implementation according to the interface standard.
JDK uses ServiceLoader to find each implementation class. It traverses all jar packages to find the file content in the META-INF/services directory to load each implementation class. This is similar to the idea of IOC, which dynamically injects implementation classes.
Using SPI must follow its standards: 1) Create a folder: META-INF/service, and place it on the classpath. 2) The configuration file is placed under META-INF/service, and the file name must be the full path of the implemented interface.
Disadvantages of ServiceLoader: 1) ServiceLoader uses lazy loading to load all the implementation classes of the interface. If you don't want to use some implementation class, but it's loaded and instantiated as well, that's a pain in the ass, I just load whatever I want to use. 2) The way to obtain an implementation class is not convenient enough, and can only be obtained in the form of Iterator.
Well, here is a brief introduction to the peripheral knowledge that I have seen and learned now (I will come back to add it later, and everyone is welcome to analyze it together).
Let's start to enter dubbo: how to use SPI in dubbo.