Implementation method of how to dynamically create interface in Java
There are many application scenarios that use dynamic interface implementation. Here are some typical applications:
1. Mybatis / JPA and other ORM frameworks can be annotated on the interface for development without writing implementation classes, and the implementation can be generated dynamically at runtime.
2. Dubbo and other distributed service frameworks, consumers only need to introduce the interface to call the remote implementation and analyze the source code. In fact, the proxy implementation of the interface is generated on the consumer side, and then the proxy calls the remote interface.
3. Spring AOP is the most typical dynamic agent.
There are two most commonly used ways to create dynamic implementations of interfaces: JDK dynamic proxy and cglib dynamic proxy.
Proxy pattern is a common design pattern. Its purpose is to provide a proxy for other objects to control access to a real object.
The agent class is responsible for preprocessing messages for the delegate class, filtering and forwarding messages, and subsequent processing after the message is executed by the delegate class.
Through the middle layer of proxy layer, the direct access to real delegate class objects can be effectively controlled. At the same time, the user-defined control strategy (spring's AOP mechanism) can be realized to obtain greater flexibility in design.
This process is demonstrated by using JDK dynamic agent and a little simple code:
1. Interface
2. Create proxy
3. The implementation when the runtime calls the method of the interface (this process is also called the method implementation of the interface)
4. Testing
In this test code, there is no implementation of the interface. Guess what the result will be?
Console printing:
Explain that the interface delegates the implementation to the agent when calling. The last thing to do is the processing in the agent:
From the above code, we can see that if we get the method and args of the interface, we can do a lot of things, such as realizing rich functions according to the method name or with the annotation on the method.
A simple example is only used to illustrate this principle. Here is another example of dynamic call of remote interface to deepen understanding.
1. Creating a proxy class and a target class requires a common service interface
2. The server side creates a remoteservice class and implements the service interface.
3. Create a call class that encapsulates client requests and returns result information
In order to facilitate the communication between the client and the server in an object-oriented manner, the information they send can be represented by the call class. A call object represents a remote call initiated by the client, including the class name or interface name, method name, method parameter type, method parameter value and method execution result of the call.
4. Create the actual business processing class in the dynamic proxy mode and implement the invocationhandler interface
5. Create a remoteserviceproxyfactory that gets the proxy class
6. Create the connector class of the underlying socket communication, which is responsible for creating intercepting, sending and accepting call objects
7. Create remote server
8. Create local client
Console print results:
This process can be simply summarized as: local interface call (client) - > local interface proxy implementation (client) - -- > remote implementation (server side)
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.