The cutting techniques in the next spring are described in the previous section. Now it's the way to use annotations to cut slices.
First of all, definitely the same as before. Need a self-active proxy annotation class Annotationawareaspectjautoproxycreator
The 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><!--own active agent annotations < Span style= "font-family:arial, Helvetica, Sans-serif;" > own initiative to find annotated classes and methods, and the previous one of their own active proxy class almost identical, the principle is probably to traverse all the annotated class, and then one by one parsing. </span><bean class= "Org.springframework.aop.aspectJ.annotation.annotationawareaspectjautoproxycreator ></bean>-<!--annotate your own active tag, and take the initiative to find classes and methods with annotations-- > <aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--The facets of the annotations need to be written by themselves. = = = Tangent + Notification is relatively simple in this class- ><bean class= "Cn.aop.aspectj2.MyAdvisor" ></bean></beans>
In the configuration file, it is not difficult to find that the most basic is a proxy object. Own the active proxy class, the aspect (can be oneself writes the class, this annotation is oneself writes, of course before that is uses the AOP inside). We need to write our own slice 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.*.* (..)) "; * * Run before the core code */@Before (CUT) public void advice () {System.out.println ("stand Alone");} /* * Run after the core code, normal exit or exception will call this */@After (CUT) public void Advice2 () {System.out.println ("block after a separate");} /* * Around notification is best not (can be replaced with before+after), because the parameters depend on the---but there are advantages. * is the information to obtain the intercepted object and corresponding method */@Around (CUT) public void Around (Proceedingjoinpoint p) throws Throwable{system.out.println (" This is around the front "+p.getkind () +", "+p.gettarget ()");p. proceed (); System.out.println ("Surround");} /* is for a function * After the previous function has finished running, this will run, * but in the method when an exception occurs, this will not run, normal exit will run */@aFterreturning (CUT) public void Afterr (Joinpoint jp) {System.out.println ("after All" +jp.getkind ());} @AfterReturning (CUT) public void After2 () {System.out.println ("all after 222");} /* This is called after an exception occurs. The precondition is = =------The preceding code does not catch this exception, * after catching an exception before. This 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 abnormal 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 calmly as the configuration file, take 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