Annotated AOP implements the source code structure:
1, first we create a new interface, love interface.
Package COM.SPRING.AOP; /** * Love interface * @author Administrator * */Publicinterface love{/* * Love method * /void fallinlove (); void Test () throws Exception;}
2, we write a person class to implement the Love interface
package COM.SPRING.AOP; /** Human object * * @author [email protected] * */ public class Person implements love{/* * rewrite dating method */ @Override public void fallinlove () {System.out . println ( "Fall in love ..." ); } @Override public void Test () throws Exception {
//TODO auto-generated Method stub
throw new Exception (); }}
3. Let's write the AOP annotation notification class "Execute method before executing method after executing method is also known as wrapping method method throws exception during execution"
Package Com.spring.aop;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.annotation.after;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;/*** Annotation Method AOP notification class * * @author [email protected] * */@Aspect Public classlovehelper{@Pointcut ("Execution (* com.spring.aop.*). *(..))")Private voidLovemethod () {}//define a pointcut //Execute the interception package com.spring.aop.* all methods before calling the method@Before ("Execution (* com.spring.aop.*). *(..))") Public voidBefore (Joinpoint point) throws Throwable {System. out. println ("Before::method"+ Point.gettarget (). GetClass (). GetName () +"."+ point.getsignature (). GetName ()); System. out. println ("You must know each other before you fall in love!" "); }//execute before and after calling the method@Around ("Execution (* com.spring.aop.*). *(..))") PublicObject around (Proceedingjoinpoint point) throws Throwable {System. out. println ("Around::method"+ Point.gettarget (). GetClass (). GetName () +"."+ point.getsignature (). GetName ());if(Point.getargs (). length > 0) {returnPoint.proceed (Point.getargs ()); }Else{returnPoint.proceed (); } }//Execute after calling the method@After ("Execution (* com.spring.aop.*). *(..))") Public voidAfterreturning (Joinpoint point) throws Throwable {System. out. println ("Method"+ Point.gettarget (). GetClass (). GetName () +"."+ point.getsignature (). GetName ()); System. out. println ("We've been talking for 5 years, and we're finally breaking up!" ");//System.out.println ("We've been talking for 5 years, and finally we're in the wedding hall!") ");}//Called when an exception is thrown@AfterThrowing (value="Execution (* com.spring.aop.*). *(..))", throwing ="Ex") Public voidDothrowing (Joinpoint Point, Throwable ex) {System. out. println ("Dothrowing::method"+ Point.gettarget (). GetClass (). GetName () +"."+ point.getsignature (). GetName () +"Throw exception"); System. out. println (Ex.getmessage ()); }}
4. Configure the Application.xml to configure the bean and AOP notification class plus a sentence enable annotation mode configuration.
<?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:p= "http://www.springframework.org/schema/p" XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:jee= "Http://www.springframework.org/schema/jee" Xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd HT Tp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/ Spring-jee-4.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.0.xsd "> <!--configuration Bean -- <Bean ID= "Person" class= "Com.spring.aop.Person"> </Bean> <!--Configuring the Notification method class - <Bean ID= "Lovehelper" class= "Com.spring.aop.LoveHelper"> </Bean> <Aop:aspectj-autoproxy /></Beans>
5, write on our test effect of the test results hehe ~ ~ ~
package com.spring.aop;import org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.spring.aop.Love; public class lovetest{public static void Main ( String[] args) {ApplicationContext appctx = new classpathxmlapplicationcontext ( "applicationcontext.xml" ); Love love = (Love) Appctx.getbean (); Love.fallinlove (); try {//Test exception capture love.test (); } catch (Exception e) {//TODO auto-generated catch Block e.printstacktrace (); } }}
6. Or write the JUnit test class
Package Com.love.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.spring.aop.love;import Junit.framework.TestCase; Public classLoveaoptest extends testcase{protected voidSetUp () {/* Prepare to start test before: Initialize, get data connection ... */}protected voidTearDown () {/* Clean up after test: Close file, close data connection ... */} Public voidTestCase2 () {ApplicationContext Appctx =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Love love = (Love) Appctx.getbean ("Person"); Love.fallinlove ();Try{//test exception captureLove.test (); }Catch(Exception e) {//TODO auto-generated catch blockE.printstacktrace (); Asserttrue (true); } }}
7, look at the console output calls our definition of the AOP interception method ~ ~ ~ OK
8. Source code: Source code Download
Spring AOP Implementation Method Three "attached source"