Implementation 1: XML-based
1 Package Com.rr.spring3.interf; Interface 23publicinterface SayHello {45 Public void SayHello (); 6 }
1 PackageCom.rr.spring3.interf.impl;//Interface Implementation class2 3 ImportCom.rr.spring3.interf.SayHello;4 5 Public classHelloImplementsSayHello {6 Public voidSayHello () {7System.out.println ("Hello");8 }9 Ten}
1 Packagecom.rr.spring3.aop;//Slice class + notification2 3 Public classHelloaspect {4 5 //front-facing notifications6 Public voidBeforeadvice () {7System.out.println ("===========before Advice");8 }9 Ten //Post Final notification One Public voidAfterfinallyadvice () { ASystem.out.println ("===========after Finally advice"); - } - the}
1 <!--Target Class -2 <BeanID= "Hello"class= "Com.rr.spring3.interf.impl.Hello"/>3 <!--Slice class -4 <BeanID= "Helloaspect"class= "Com.rr.spring3.aop.HelloAspect"/>5 6 <Aop:config>7 <!--Reference Slice class -8 <Aop:aspectref= "Helloaspect">9 <!--entry point -Ten <Aop:pointcutID= "PC"expression= "Execution (* com.rr.spring3.interf.*.* (..))"/> One <!--reference pointcuts, specifying notifications - A <Aop:beforePointcut-ref= "PC"Method= "Beforeadvice"/> - <Aop:afterpointcut= "Execution (* com.rr.spring3.interf.*.* (..))"Method= "Afterfinallyadvice"/> - </Aop:aspect> the </Aop:config>
Implementation 2: Based on Java5 annotation @Aspect
Interfaces and interfaces are implemented in a similar
1 PackageCOM.RR.SPRING3.AOP;//Slice class + notification2 3 Importorg.aspectj.lang.annotation.After;//java5 annotations4 ImportOrg.aspectj.lang.annotation.Aspect;5 ImportOrg.aspectj.lang.annotation.Before;6 ImportOrg.aspectj.lang.annotation.Pointcut;7 8 @Aspect9 Public classHelloaspect {Ten One@Pointcut ("Execution (* com.rr.spring3.interf.*.* (..))") A Private voidSelectAll () {} - - //front-facing notifications the@Before ("SelectAll ()") - Public voidBeforeadvice () { -System.out.println ("===========before Advice"); - } + - //Post Final notification +@After ("SelectAll ()") A Public voidAfterfinallyadvice () { atSystem.out.println ("===========after Finally advice"); - } - -}
1 <Aop:aspectj-autoproxy/> <!--opening annotations --2 <!--Target Class -3 <BeanID= "Hello"class= "Com.rr.spring3.interf.impl.Hello"/>4 <!--Slice class -5 <BeanID= "Helloaspect"class= "Com.rr.spring3.aop.HelloAspect"/>
Test results:
Two ways to implement spring----> AOP