We use a simple example to demonstrate AOP in Spring. This is an example of log. In fact, log is a very bad example for AOP, here we only explain the use of Spring Ao.
First, let's first create a selfceptor.
This class must inherit the org.aopalliance.Intercept. MethodInterceptor interface. Spring's AOP framework is implemented with reference to AopAlliance, so our MyInterceptor should inherit the interface in this standard.
This interface has only one way to implement:
Public Object Invoke (MethodInvocation MethodInvocation) throws throwable;
Here is our MyIntercptor:
Public class myinterceptor imports methodinterceptor {
Private final log logger = logfactory.getlog (getclass ());
Public Object Invoke (MethodInvocation MethodInvocation) throws throwable
{
Logger.info ("Beginning Method (1):" MethodInvocation.getMethod (). getDeclaringclass () "." MethodInvocation.getMethod (). getname () ";)
Long StartTime = system.currenttimemillis ();
Try
{
Object result = methodinvocation.proceed ();
Return Result;
}
Finally {
Logger.info ("Ending Method (1):" MethodInvocation.getMethod (). getDeclaringclass () " " MethodInvocation.getMethod (). getname () "()");
Ogger.info ("Method Invocation Time (1) (System.currentTimeMillis () - StartTime) " MS. ");
}
}
}
The following two lines of code are required:
Object results = method (); return result; the process of the entire program is like this:
1, first execute in Object Result = methodInvocation.proceed (); the previous code;
2, then execute Object Result = methodinvocation.proceed ();, it handles the execution control over the next interceptor in the Interceptor Stack, if there is no time to give a real business method;
3, then execute returnrate; previous code;
4, finally executing Return Result;, it retracted the interceptor above it, if you don't have it, you will exit Interceptor Stack.
Second, write our business objects and their interfaces to facilitate our business interface only one Hello method: public interface businessinterFace
{
Public void hello ();
}
The code for the business object is as follows:
Public Class BusinessInterfaceImpl Implements BusinessInterface
{
Public void Hello ()
{
System.out.Println ("Hello Spring AOP.");
}
}
Third, next, let's take a look at how to use the interceptor written. We use the business object as a Target of AOP:
Then the Interceptor is declared in the bean definition:
Finally, let's declare true business objects, by using its interface and Spring ProxyFactoryBean:
com.rst.spring.testaop.businessInterface
MyInterceptor
BusinesTarget
Here you need to explain two points:
Proxyinterfaces: is the actual interface of our business object;
InterceptorNames: Defines all interceptors execution order, where the Target of the business object is the last one of the List. Remember to put the Target of the business object in List, otherwise your business object will not work.
Fourth, finally, write our test class
ClasspathResource Resource = New ClassPathResource ("COM / RST / Spring / Testaop / AOP_Bean.xml");
Xmlbeanfactory beanfactory = new xmlbeanfactory (resource);
BeanFactory.getBean ("BusinessBean"); "BusinessBean";
BusinessBean.hello ();
Everything you can see the corresponding information on the log.
The following is the effect of the attachment source code:
2004-09-08 16: 04: 51, 210 Info - Beginning Method (1): Interface com.rst.spring.testaop.businessinterface.hello ()
2004-09-08 16: 04: 51, 210 Info - Beginning Method (2): interface com.rst.spring.testaop.businessInterface.hello () Hello Spring AOP.
2004-09-08 16: 04: 51, 210 Info - Ending Method (2): interface com.rst.spring.testaop.businessinterface.hello ()
2004-09-08 16: 04: 51, 210 Info - Ending Method (1): interface com.rst.spring.testaop.businessinterface.hello ()
2004-09-08 16: 04: 51, 210 Info - Method Invocation Time (1): 0 ms. Source code requires Spring.jar, AopAllience.jar, commons-logging.jar.
Let me introduce the dynamic proxy in JDK: Dynamic Proxy.
Spring AOP is implemented using dynamic proxy, including another famous AOP framework nanning. First of all, our interface and implementation class:
public interface Speakable {public void speak ();} public class SpeakHello implements Speakable {private Logger logger = Logger.getLogger (getClass ()); public void speak () {logger.info ( "hello");}}
Then the Proxy class:
Public Class SpeakProxy Implements InvocationHandler {
Private logger logger = logger.getlogger (this.getclass ());
Private Object Callee;
Private SpeakProxy (Object Callee) {
THIS.CALLEE = Callee;
}
Public Static Speakable getProxy (Object Callee) {
Return (Speakable) proxy.newproxyinstance (Callee.getClass (). getClassLoader (),
Callee.getClass (). getInterfaces (),
NEW SpeakProxy (Callee);
}
Public Object Invoke (Object Proxy, Method Method, Object [] args throws throwable {
Object result = null;
Logger.info ("Before Method Invoke." ");
Try {
Result = method.invoke (Callee, Args);
} Catch (IllegalargumentException E) {
Logger.info (Method.getName () "HAS ILLEGAL ARGUMENT EXCEPTION.");
E.PrintStackTrace ();
} Catch (IllegaCcessException E) {
Logger.info (Method.getName () "HAS ILLEGAL Access Exception.");
E.PrintStackTrace ();
} Catch (invocationTargeTexception E) {
Logger.info (Method.getName () "HAS Invocation Target Exception.");
E.PrintStackTrace ();
} finally {
Logger.info ("After Mthod Invoke." ");
}
Return Result;
}
}
Note When NewProxy is returned, you can only return to Object, then call the interface to Cast needs to be invoked.
Here is simplicity and clear, only return SPEAKABLE.
Last to how to use this proxy:
Public class test {
Public static void main (String [] args) {
Speak Speak = SpeakProxy.getProxy (New SpeakHello ()); Speak.Speak ();
}
}
If everything is normal, you can see the following information:
2004-09-09 10: 35: 16,655 Info - Before Method Invoke. SPEAK ()
2004-09-09 10: 35: 16,665 Info - Hello
2004-09-09 10: 35: 16,665 Info - After mthod invoke. Speak ()
(Use log4j.jar in the program, you can change it directly to system.out.println easier)
Ezerg Programming