In some Java projects, Mapperscannerconfigurer is used in MyBatis and spring consolidation, which automatically generates an interface based dynamic proxy class through a reverse proxy.
In view of this, this article analyzes the Java dynamic Proxy.
This article uses a dynamic proxy to simulate the interceptor that handles transactions.
Interface:
Public interface UserService {public
void AddUser ();
public void Removeuser ();
public void Searchuser ();
}
Implementation class:
public class Userserviceimpl implements UserService {public
void AddUser () {
System.out.println ("Add User"); c10/>} public
void Removeuser () {
System.out.println ("Remove user");
}
public void Searchuser () {
System.out.println ("Search user");
}
There are 2 ways to implement Java dynamic proxies
1.JDK Self-band dynamic agent
Dynamic proxies with JDK are required to understand the Invocationhandler interface and proxy classes, all under the Java.lang.reflect package.
Invocationhandler Introduction:
Invocationhandler is an interface implemented by the invocation handler of the proxy instance.
Each proxy instance has an associated invocationhandler. When a method is invoked on a proxy instance, this method invokes the Invocationhandler invoke method.
Proxy Introduction:
Proxy provides static methods for creating dynamic proxy classes and instances.
Instance (analog AOP processing transaction):
public class Transactioninterceptor implements Invocationhandler {
private Object target;
public void Settarget (Object target) {
this.target = target;
}
@Override Public
Object Invoke (Object Proxy, Method method, object[] args) throws Throwable {
System.out.println ("Start Transaction");
Method.invoke (target, args);
System.out.println ("End Transaction");
return null;
}
}
Test code:
public class Testdynamicproxy {
@Test public
void Testjdk () {
transactioninterceptor Transactioninterceptor = new Transactioninterceptor ();
UserService userservice = new Userserviceimpl ();
Transactioninterceptor.settarget (userservice);
UserService userserviceproxy =
(userservice) proxy.newproxyinstance (
userservice.getclass (). getClassLoader (), Userservice.getclass (), getinterfaces ()
,
transactioninterceptor);
Userserviceproxy.adduser ();
}
Test results:
Start Transaction
Add user end
Transaction
Obviously, when we make a method call by Userserviceproxy this proxy class, the transaction is opened and closed before and after the method call.
2. Third-party library Cglib
Cglib is a powerful, high-performance, high-quality code generation library for extending Java classes and implementing Java interfaces during runtime.
The biggest difference between it and the dynamic proxy for JDK is that:
The JDK dynamic proxy is for the interface, and the cglib is to implement the proxy for the class, the principle of cglib is to generate a subclass of the specified target class and overwrite the method implementation enhancement, but because of inheritance, the final decorated class cannot be represented.
The instance code is as follows:
The public class Userservicecallback implements Methodinterceptor {
@Override
the public object intercept (object o, Method method, object[] args, Methodproxy methodproxy) throws Throwable {
System.out.println ("Start Transaction by CG Lib ");
Methodproxy.invokesuper (o, args);
System.out.println ("End Transaction by Cglib");
return null;
}
}
Test code:
public class Testdynamicproxy {
@Test public
void Testcglib () {
enhancer enhancer = new enhancer ();
Enhancer.setsuperclass (userserviceimpl.class);
Enhancer.setcallback (New Userservicecallback ());
Userserviceimpl proxy = (Userserviceimpl) enhancer.create ();
Proxy.adduser ();
}
Test results:
Start Transaction by cglib
Add user end
Transaction by Cglib
Interested readers can actually test the example of this article, I believe there will be a great harvest.