The previous examples are provided for everyone to learn. As we all know, spring's AOP function actually implements Java's dynamic proxy function, the following describes the Java Dynamic proxy, the implementation of AOP by cglib in spring, and the configuration of AOP in spring (configuration file mode and annotation mode ).
1. Dynamic Proxy: Before calling an interface, intercept the interface to be proxy in advance, intercept some things, and continue to call the interface method after processing, without affecting the normal logic
Can be used for permission Control
2. Implementation example (related jar is not listed)
1 Interface
Package com. mooing. Service;
Public interface myservice {
Public void save ();
Public void findall ();
Public void Update ();
Public void Delete ();
}
2 Implementation class
Package com. mooing. Service;
Public class myserviceimpl implements myservice {
Private string temp = NULL;
Public myserviceimpl (){
}
Public myserviceimpl (string temp ){
This. Temp = temp;
}
Public void save (){
System. Out. println ("this is save ()");
}
@ Override
Public void findall (){
System. Out. println ("this is findall ()");
}
@ Override
Public void Update (){
System. Out. println ("this is Update ()");
}
@ Override
Public void Delete (){
System. Out. println ("this is delete ()");
}
Public String gettemp (){
Return temp;
}
}
3 proxy
Package com. mooing. Service;
Import java. Lang. Reflect. invocationhandler;
Import java. Lang. Reflect. method;
Import java. Lang. Reflect. proxy;
/**
Create a proxy class for the target object
* Proxy class, which must implement the invocationhandle Interface
* @ Author mooing
*
*/
Public class myproxyservice implements invocationhandler {
Private object targetobj;
/**
* Create a proxy class for the target object
*
* Input the proxy class to create the proxy instance of this class
*
* @ Param targetobj: target object
* @ Return returns the proxy class of the target object.
*/Public object createproxyinstances (Object targetobj ){
This.tar getobj = targetobj;
/**
* Parameter 1: class loaders for proxy objects
* Parameter 2: All interfaces of the proxy object
* Parameter 3 indicates the Interception Method in the class to be triggered (the callback method invoke is used to process the operations before the actually called method). This indicates the current class
*/
Return proxy. newproxyinstance (targetobj. getclass (). getclassloader (), targetobj. getclass (). getinterfaces (), this );
}
/**
* Callback Method
*/
@ Override
Public object invoke (Object proxy, method, object [] ARGs) throws throwable {
// Myservice MS = (myservice) createproxyinstances (New myserviceimpl ());
// Myserviceimpl myservice = (myserviceimpl) targetobj;
// Object result = NULL;
// If (myservice. gettemp ()! = NULL ){
// Result = method. Invoke (targetobj, argS );
//}
// Return result;
System. Out. println ("Access Interception Method ");
Return method. Invoke (targetobj, argS );
}
}
Test class
Package com. mooing. test;
Import org. JUnit. test;
Import com. mooing. Service. myproxyservice;
Import com. mooing. Service. myservice;
Import com. mooing. Service. myserviceimpl;
Public class myproxytest {
@ Test
Public void testjdkproxy (){
Myproxyservice proxy = new myproxyservice ();
// Proxy purpose: If you assign a value to temp in the myserviceimpl class, the interface method is called. Otherwise, the method is not called, similar to permission control.
Myservice MS = (myservice) proxy. createproxyinstances (New myserviceimpl ("XXX "));
Ms. Delete ();
}
}