Two ways to implement spring----> AOP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.