Three configuration beans configuration files for agents
<?XML version= "1.0" encoding= "UTF-8"?><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"xsi:schemalocation= "http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-bea Ns-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/SPRING-AOP -3.2.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spri Ng-context-3.2.xsd "> <BeanID= "istudentservice"class= "com.my.servic.impl.StudentServiceImpl"></Bean> <BeanID= "iempservice"class= "com.my.servic.impl.EmpServiceImpl"></Bean> <BeanID= "logadivce"class= "com.my.advice.LogAdivce"></Bean> <BeanID= "customeradvice"class= "com.my.advice.CustomerAdvice"></Bean> <BeanID= "annotationadvice"class= "com.my.advice.AnnotationAdvice"></Bean> <Aop:config> <Aop:pointcutexpression= "execution (* com.my.servic.impl.*.* (..))"ID= "pointcut" /> <Aop:advisorAdvice-ref= "logadivce"Pointcut-ref= "pointcut" /> </Aop:config> <!--Custom Configuration Notifications - <Aop:config> <Aop:aspectref= "customeradvice"> <Aop:pointcutexpression= "execution (* com.my.servic.impl.*.add (..))"ID= "pointcut1" /> <Aop:pointcutexpression= "execution (* com.my.servic.impl.*.delete (..))"ID= "pointcut2" /> <Aop:aroundMethod= "roundmethod"Pointcut-ref= "pointcut1" /> <Aop:beforeMethod= "methodbefore"Pointcut-ref= "pointcut1" /> <Aop:afterMethod= "methodafter"Pointcut-ref= "pointcut2" /> </Aop:aspect> </Aop:config> <!--Scan Annotation Components - <Context:component-scanBase-package="*"></Context:component-scan> <!--Annotation Method - <Aop:aspectj-autoproxy/> <!--using Proxyfactorybean - <BeanID= "logproxy"class= "org.springframework.aop.framework.ProxyFactoryBean"> < propertyname= "proxyinterfaces"> <List> <value>Com.my.servic.IStudentService</value> <value>Com.my.servic.IEmpService</value> </List> </ property> < propertyname= "target"> <refLocal= "istudentservice" /> </ property> < propertyname= "interceptornames"> <List> <value>Logadivce</value> </List> </ property> </Bean></Beans>
Annotation notation
packagecom.my.advice;Importorg.aspectj.lang.JoinPoint;Importorg.aspectj.lang.ProceedingJoinPoint;Importorg.aspectj.lang.annotation.Around;Importorg.aspectj.lang.annotation.Aspect;Importorg.aspectj.lang.annotation.Before;Importorg.springframework.stereotype.Component, @Component @aspect public classAnnotationadvice {@Before ("execution (* com.my.servic.impl.*.* (..))") public voidbefore (joinpoint Jp) {System.out.println ("before the method Call: "); System.out.println (jp.gettarget ()+ "+++>>>" +jp.getsignature () + "" +Jp.getargs ()); } @Around ("execution (* com.my.servic.impl.*.* (..))") public voidround (proceedingjoinpoint pjp) {System.out.println ("surround the Front. "); Try{pjp.proceed (); } Catch(throwable E) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("round the back before ... "); }}
Custom notation
packagecom.my.advice;Importorg.aspectj.lang.JoinPoint;Importorg.aspectj.lang.ProceedingJoinPoint; public classCustomeradvice { public voidmethodbefore (joinpoint jp) {System.out.println ("before the method Call: "); System.out.println (jp.gettarget ()+ "+++>>>" +jp.getsignature () + "" +Jp.getargs ()); } public voidmethodafter (joinpoint jp) {System.out.println ("after the method Call: "); } public voidRoundmethod (proceedingjoinpoint pjp) {System.out.println ("surround the Front. "); Try{pjp.proceed (); } Catch(throwable E) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("surround it. "); }}
General implementation of interface notation
packagecom.my.advice;Importjava.lang.reflect.Method;Importjava.text.SimpleDateFormat;Importjava.util.Date;Importorg.aopalliance.intercept.MethodInterceptor;Importorg.aopalliance.intercept.MethodInvocation;Importorg.springframework.aop.AfterReturningAdvice;Importorg.springframework.aop.MethodBeforeAdvice;Importcom.my.bean.Emp;Importcom.my.bean.Student; public classLogadivceImplementsmethodbeforeadvice, afterreturningadvice, methodinterceptor {@Override public voidbefore (method, object[] arg1, Object target)throwsthrowable {simpledateformat SF=NewSimpleDateFormat ("YYYY-MM-DD hh:mm:ss"); System.out.println ("target object" + target.getclass (). getName () + "+ method.getname () +" in "+ Sf.format (NewDate ()) + "was called"); for(Object Obj:arg1) {if(obj.getclass (). getName (). equals ("com.my.bean.Student") {Student Stu=(Student) obj; System.out.println (The parameter is: "+Stu.getname ()); } Else if(obj.getclass (). getName (). equals ("com.my.bean.Emp") ) {Emp e=(Emp) obj; System.out.println (The parameter is: "+E.getname ()); } }} @Override public voidafterreturning (object arg0, Method arg1, object[] arg2, Object Arg3)throwsThrowable {//TODO auto-generated Method Stub} @Override publicObject Invoke (methodinvocation Methodinvocation)throwsthrowable {System.out.println ("surround the Front. "); Object obj=Methodinvocation.proceed (); System.out.println ("surround it. "); returnobj; }}
Java EE notes (12)