Reference: https://docs.spring.io/spring-framework/docs/4.3.9.RELEASE/spring-framework-reference/htmlsingle/# Aop-aj-ltw-first-example
https://www.ibm.com/developerworks/cn/java/j-lo-springaopcglib/(Spring AOP Implementation principle and CGLIB application)
http://blog.csdn.net/a128953ad/article/details/50509437 (comparative analysis of differences between Spring AOP and AspectJ)
Aspect is a concrete implementation of AOP programming
There are two ways to implement aspect:
1. Using the AJC compiler to generate proxy classes in the compiler
2. Using AspectJ LTW to generate agents at class load time
The main bean
Public class Demobean { publicvoid run () { System.out.println ("Run"); } publicvoid run1 () { System.out.println ("run1 ..."); Public void throws Exception { TimeUnit.SECONDS.sleep (2); System.out.println ("run2 ...");} }
Aspect
ImportOrg.aspectj.lang.ProceedingJoinPoint;ImportOrg.aspectj.lang.annotation.Around;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Pointcut;ImportOrg.springframework.util.StopWatch; @Aspect Public classprofilingaspect {@Around ("Profilemethod ()") PublicObject profile (Proceedingjoinpoint PJP)throwsthrowable {StopWatch SW=NewStopWatch (GetClass (). Getsimplename ()); Try{Sw.start (Pjp.getsignature (). GetName ()); returnpjp.proceed (); } finally{sw.stop (); System.out.println (Sw.prettyprint ()); }} @Pointcut ("Execution (* com.jxufe.study.spring.aspect). *.*(..))") Public voidProfilemethod () {}}
Meta-inf/aop.xml (this path and name are OK)
<!DOCTYPE aspectj Public "-//aspectj//dtd//en" "Http://www.eclipse.org/aspectj/dtd/aspectj.dtd"> <AspectJ> <Weaver> <!--Only weave classes in our Application-specific packages - <includewithin= "com.jxufe.study.spring.aspect.*"/> </Weaver> <Aspects> <!--weave in just this aspect - <Aspectname= "Com.jxufe.study.spring.aspect.ProfilingAspect"/> </Aspects> </AspectJ>
The last Aspect.xml
<?XML version= "1.0" encoding= "GBK"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.5.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.5.xsd "> <Context:load-time-weaver/> <BeanID= "Demobean"class= "Com.jxufe.study.spring.aspect.DemoBean"></Bean></Beans>
Test class
public static void main (string[] args) throws Exception {ApplicationContext CTX = new classpathxml ApplicationContext ("/meta-inf/aspect.xml", Main. Class /* Demobean Demobean = (Demobean) ctx.getbean ("de"); */ Demobean demobean = new Demobean (); Demobean.run (); Demobean.run1 (); Demobean.run2 ();
Add JVM parameters at runtime-javaagent:c:\users\1\.m2\repository\org\springframework\spring-instrument\4.3.9.release\ Spring-instrument-4.3.9.release.jar
Output Result:
runstopwatch ' profilingaspect ': Running time (Millis) = 0------------------ -----------------------ms % Task name ---- -------------------------------------00000 ' profilingaspect ': Running time (Millis) = 1-----------------------------------------ms % Task name ----------------------------------------- 00001 100% run1disconnected from the target VM, address: ' 127.0.0.1:52489 ', Transpo RT: ' Socket ' run2 ... StopWatch ' profilingaspect ': Running time (Millis) = 2003-----------------------------------------ms % Task name ---------------------------------------- -02003 100% run2
In the test class here, the bean uses demobean Demobean = new Demobean (), but the result is an AOP effect.
A simple example of using spring's ASPECTJ LTW