Spring AOP Essence (4)

Source: Internet
Author: User

This is a major look at how spring AOP is surrounded by notifications. Spring AOP bracketing notifications are similar in functionality to Pre-notification plus post-notification, but there is a difference: bracketing notifications can modify the return value, and you can block and replace the execution of the target Method. The enclosing notification in spring is an interceptor that implements the Methodinterceptor Interface. Spring surround notification has a wide range of applications, such as remote agents and transaction management, which are done by Interceptors. In addition, interceptors are a good way to dissect a program's Operation. The following is a time-consuming process for monitoring the execution of a business method with spring AOP bracketing notifications./**
* Business Components
*/
publicclassWorkerbean {

publicvoidDosomework (intnooftimes) {
for(intx = 0; X < nooftimes; X + +) {
Work ();
}
}

PrivatevoidWork () {
System.out.print ("");
}
}Importjava.lang.reflect.Method;

Importorg.aopalliance.intercept.MethodInterceptor;
Importorg.aopalliance.intercept.MethodInvocation;
Importorg.springframework.util.StopWatch;

/**
* interceptor, Implementation Method bracketing notification
*/
publicclassProfilinginterceptorImplementsMethodinterceptor {

publicObject Invoke (methodinvocation Invocation)throwsThrowable {
//start a stop watch
StopWatch SW =NewStopWatch ();
//run Timer
Sw.start (invocation.getmethod (). getName ());
//execute Business Method
Object returnvalue = Invocation.proceed ();
//stop Timer
Sw.stop ();
//junk Information Output
Dumpinfo (invocation, Sw.gettotaltimemillis ());
//return Business method return value
returnreturnvalue;
}

/**
* Garbage information input method, actually output is the method to run the timing information
*/
PrivatevoidDumpinfo (methodinvocation invocation,LongMs) {
//get called method
Method m = Invocation.getmethod ();
//gets the object to which the called method belongs
Object target = Invocation.getthis ();
//gets The parameters of the called Method
object[] args = invocation.getarguments ();

System.out.println ("the Method executed:"+ M.getname ());
System.out.println ("type of object:"+ Target.getclass (). getName ());

System.out.println ("parameter of the method:");
for(intx = 0; X < args.length; X + +) {
System.out.print (">"+ args[x]);
}
System.out.print ("\ n");

System.out.println ("crawl method Run time:"+ MS +"ms");
}
}Importorg.springframework.aop.framework.ProxyFactory;

/**
* Client Test method
*/
publicclassProfilingexample {

public StaticvoidMain (string[] Args) {
//create proxy Object
Workerbean Bean = Getworkerbean ();
//call The business method on the proxy object
Bean.dosomework (10000000);
}

/**
* Proxy Object Factory
*/
PrivateStaticWorkerbean Getworkerbean () {
//create target Object
Workerbean target =NewWorkerbean ();
//build Agent Object Factory
Proxyfactory factory =NewProxyfactory ();
Factory.settarget (target);
Factory.addadvice (NewProfilinginterceptor ());

//production of a proxy object
return(workerbean) Factory.getproxy ();
}
Run result:-Using JDK 1.4 collections
Method Implemented: Dosomework
Type of Object: Com.apress.prospring.ch6.profiling.WorkerBean
Parameters of the Method:
> 10000000
Crawl method Run Time: 3453 ms

Process finished with exit code 0 from the results of the output, you can see the method name, parameters, owning class, and the time it takes to execute the METHOD. In addition to the Org.springframework.util.StopWatch class, this is a timer class that can basically get the time between the Timer class start () and stop () two method calls. You can see the API documentation for spring in Detail. In addition, I also have org.apache.common.lang.time.StopWatch in Apache Commons bag Inside.

http://lavasoft.blog.51cto.com/62575/75342/

Spring AOP Essence (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.