The cutting techniques in the next spring are described in the previous section, and now we are going to use annotations to cut slices.
First of all, it must be the same as before, requiring an automatic proxy annotation class Annotationawareaspectjautoproxycreator
Code in the configuration file:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context /SPRING-CONTEXT-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd "><bean id=" person "class=" Cn.aop.aspectj2.Person "></bean><!--automatic proxy annotations <span Style= "Font-family:arial, Helvetica, Sans-serif;" > automatically to find annotated classes and methods, similar to the previous automatic proxy class, the principle is probably to traverse all annotated classes, and then do one by one parsing. </span><bean class= "Org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" ></bean>- <!--annotate automatic tags, automatically find classes and methods with annotations--<aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--facets of annotations You need to write your own slices. = = = Tangent + notice in the class relative to the previous, relatively simple--><bean class= "Cn.aop.aspectj2.MyAdvisor" ></bean></beans>
In the configuration file, we are not difficult to find, the most basic is a proxy object, automatic proxy class, slice (can be the class you write, this annotation is written by itself, of course, the previous one is in AOP). We need to write a slice of our own class
Slice class
Package Cn.aop.aspectj2;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.annotation.after;import Org.aspectj.lang.annotation.afterreturning;import Org.aspectj.lang.annotation.afterthrowing;import Org.aspectj.lang.annotation.around;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import Org.aspectj.lang.annotation.Pointcut; @Aspectpublic class Myadvisor {private final String cut= "Execution (* Cn.aop.aspectj2.*.* (..)) "; * * * Execute before the core code */@Before (CUT) public void advice () {System.out.println ("stand Alone");} /* * After the core code execution, normal exit or exception will call this */@After (CUT) public void Advice2 () {System.out.println ("block after a separate");} /* * Around notifications are best not (can be replaced with before+after), because parameters depend on---but also benefit, * is to get the intercepted object and the corresponding method of information */@Around (CUT) public void Around ( Proceedingjoinpoint p) throws Throwable{system.out.println ("This is wrapping before" +p.getkind () + "," +p.gettarget () ");p. proceed (); System.out.println ("Surround");} /* is for a function * This will only execute after the previous function has completed, * but the method will not execute after an exception occurs, and the normal exit will execute */@aFterreturning (CUT) public void Afterr (Joinpoint jp) {System.out.println ("all After" +jp.getkind ());} @AfterReturning (CUT) public void After2 () {System.out.println ("all after 222");} /* This is called after an exception, if = =------The preceding code does not catch the exception, * after catching an exception, here is not called, generally in the log used more. * */@AfterThrowing (CUT) public void Throwex () throws Exception{system.out.println ("Out of Exception");}}
Person class
Package Cn.aop.aspectj2;public class Person {public void say () {//integer.parseint ("AA");//test for exception System.out.println ( "... This is say. ");} public void Run () {System.out.println ("This is the Run method in person");}}
Test Class
public class Demo2 {@Testpublic void Test () {//the same device takes the configuration file, takes out the object ApplicationContext app = new Classpathxmlapplicationcontext ("Cn/aop/aspectj2/aspectj2.xml"); Person P=app.getbean (Person.class);p. Run ();p. Say ();}}
Java EE------Spring-----"ASPECTJ notes