SPRING in ACTION 4th Note-fourth Chapter aspect-oriented spring-007-defines the around of the slice advice

Source: Internet
Author: User
Tags throwable money back

I. Annotations @aspectj form

1.

 PackageCom.springinaction.springidol;ImportOrg.aspectj.lang.ProceedingJoinPoint;ImportOrg.aspectj.lang.annotation.Around;ImportOrg.aspectj.lang.annotation.Aspect;Importorg.aspectj.lang.annotation.Pointcut; @Aspect Public classaroundaudience {@Pointcut ("Execution (* com.springinaction.springidol.Performer.perform (..))")   Public voidperformance () {}//<start id= "Audience_around_bean"/>@Around ("Performance ()")   Public voidwatchperformance (Proceedingjoinpoint joinpoint) {Try{System.out.println ("The audience is taking their seats."); System.out.println ("The audience is turning off their cellphones"); LongStart =System.currenttimemillis ();      Joinpoint.proceed (); LongEnd =System.currenttimemillis (); System.out.println ("Clap clap clap clap Clap"); System.out.println ("The performance took" + (end-start)+ "milliseconds."); } Catch(Throwable t) {System.out.println ("Boo! We Want our Money back! "); }  }  //<end id= "Audience_around_bean"/>}

2.

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5Xsi:schemalocation= "Http://www.springframework.org/schema/beans6http//www.springframework.org/schema/beans/spring-beans.xsd7http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP8http//www.springframework.org/schema/aop/spring-aop.xsd ">9 Ten<bean id= "Eddie" One       class= "Com.springinaction.springidol.Instrumentalist" > A<property name= "Instrument" > -<beanclass= "Com.springinaction.springidol.Guitar"/> -</property> the</bean> -  -<beanclass= "Com.springinaction.springidol.AroundAudience"/> -  +<aop:aspectj-autoproxy/> -  +</beans>

3. Or use a Java configuration file

1  PackageCom.springinaction.springidol;2 3 ImportOrg.springframework.context.annotation.Bean;4 ImportOrg.springframework.context.annotation.ComponentScan;5 Importorg.springframework.context.annotation.Configuration;6 ImportOrg.springframework.context.annotation.EnableAspectJAutoProxy;7 8 @Configuration9 @EnableAspectJAutoProxyTen @ComponentScan One  Public classconfig{ A @Bean -      Publicaroundaudience aroundaudience () { -         return Newaroundaudience (); the     } -      - @Bean -      Publicperformer Eddie () { +Instrumentalist instrumentalist =Newinstrumentalist (); - instrumentalist.setinstrument (instrument ()); +         returninstrumentalist; A     } at      - @Bean -      PublicInstrument instrument () { -         return NewGuitar (); -     } -}

4. Testing

1  PackageCom.springinaction.springidol;2 3 Importorg.junit.Test;4 ImportOrg.junit.runner.RunWith;5 Importorg.springframework.beans.factory.annotation.Autowired;6 ImportOrg.springframework.context.ApplicationContext;7 Importorg.springframework.test.context.ContextConfiguration;8 ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;9 Ten@RunWith (Springjunit4classrunner.class) One //@ContextConfiguration ("Spring-aroundadvice.xml") A@ContextConfiguration (Classes=config.class)//Specify the configuration file -  Public classAroundadvicetest { - @Autowired the ApplicationContext context; -  - @Test -    Public voidAudienceshouldapplaud ()throwsException { +Performer Eddie = (performer) Context.getbean ("Eddie"); - eddie.perform (); +   } A  at}

Results:

0 milliseconds.

Second, the form of XML <aop:config></aop:config>

1.

1  PackageCom.springinaction.springidol;2 3 ImportOrg.aspectj.lang.ProceedingJoinPoint;4 5  Public classAroundaudience {6   //<start id= "Audience_around_bean"/>7    Public voidwatchperformance (Proceedingjoinpoint joinpoint) {8     Try {9System.out.println ("The audience is taking their seats.");TenSystem.out.println ("The audience is turning off their cellphones"); One       LongStart = System.currenttimemillis ();//<co id= "Co_beforeproceed"/> A  -Joinpoint.proceed ();//<co id= "Co_proceed"/> -        the       LongEnd = System.currenttimemillis ();//<co id= "Co_afterproceed"/> -System.out.println ("Clap clap clap clap Clap"); -SYSTEM.OUT.PRINTLN ("The performance took" + (end-start) -+ "milliseconds."); +}Catch(Throwable t) { -System.out.println ("Boo! We Want our Money back! ");//<co id= "co_afterexception"/> +     } A   } at   //<end id= "Audience_around_bean"/> -}

2.

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5Xsi:schemalocation= "Http://www.springframework.org/schema/beans6http//www.springframework.org/schema/beans/spring-beans.xsd7http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP8http//www.springframework.org/schema/aop/spring-aop.xsd ">9 Ten<bean id= "Eddie" One       class= "Com.springinaction.springidol.Instrumentalist" > A<property name= "Instrument" > -<beanclass= "Com.springinaction.springidol.Guitar"/> -</property> the</bean> -  -<!--<start id= "Audience_bean"/>-- -<bean id= "Audience" +       class= "Com.springinaction.springidol.AroundAudience"/> -<!--<end id= "Audience_bean"/>-- +  A<!--<start id= "Audience_aspect"/>-- at<aop:config> -<aop:aspect ref= "Audience" > -<aop:pointcut id= "Performance" expression= -"Execution (* com.springinaction.springidol.Performer.perform (..))" -/> -  in<Aop:around -pointcut-ref= "Performance" tomethod= "Watchperformance"/> +</aop:aspect> -</aop:config> the<!--<end id= "Audience_aspect"/>-- *  $</beans>

SPRING in ACTION 4th Note-fourth Chapter aspect-oriented spring-007-defines the around of the slice advice

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.