关于Execution 表达式
生活随笔
收集整理的這篇文章主要介紹了
关于Execution 表达式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?
name-pattern(param-pattern) throws-pattern?)
modifiers-pattern:方法的操作權限
ret-type-pattern:返回值【必填】
declaring-type-pattern:方法所在的包
name-pattern:方法名【必填】
parm-pattern:參數名
throws-pattern:異常
目前SpringAOP 配置有兩種形式,這個小伙伴們應該都非常清楚,我這里就不做過多贅述,如下Annotation 配置形式:
/*** Annotation版Aspect切面Bean*/ //聲明這是一個組件 @Component //聲明這是一個切面Bean @Aspect @Slf4j public class AnnotaionAspect {//配置切入點,該方法無方法體,主要為方便同類中其他方法使用此處配置的切入點@Pointcut("execution(* com.leon.pattern.spring.aop.service..*(..))")public void aspect(){ }/** 配置前置通知,使用在方法aspect()上注冊的切入點* 同時接受JoinPoint切入點對象,可以沒有該參數*/@Before("aspect()")public void before(JoinPoint joinPoint){log.info("before通知 " + joinPoint);}//配置后置通知,使用在方法aspect()上注冊的切入點@After("aspect()")public void after(JoinPoint joinPoint){log.info("after通知 " + joinPoint);}//配置環繞通知,使用在方法aspect()上注冊的切入點@Around("aspect()")public void around(JoinPoint joinPoint){long start = System.currentTimeMillis();try {((ProceedingJoinPoint) joinPoint).proceed();long end = System.currentTimeMillis();log.info("around通知 " + joinPoint + "\tUse time : " + (end - start) + " ms!");} catch (Throwable e) {long end = System.currentTimeMillis();log.info("around通知 " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : " + e.getMessage());}}//配置后置返回通知,使用在方法aspect()上注冊的切入點@AfterReturning("aspect()")public void afterReturn(JoinPoint joinPoint){log.info("afterReturn通知 " + joinPoint);}//配置拋出異常后通知,使用在方法aspect()上注冊的切入點@AfterThrowing(pointcut="aspect()", throwing="ex")public void afterThrow(JoinPoint joinPoint, Exception ex){log.info("afterThrow通知 " + joinPoint + "\t" + ex.getMessage());}}Xml 配置形式:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><aop:aspectj-autoproxy proxy-target-class="true"/><bean id="xmlAspect" class="com.leon.pattern.spring.aop.aspect.XmlAspect"></bean><!-- AOP配置 --><aop:config><!-- 聲明一個切面,并注入切面Bean,相當于@Aspect --><aop:aspect ref="xmlAspect"><!-- 配置一個切入點,相當于@Pointcut,用來切面規律一種語言 --><aop:pointcut expression="execution(* com.leon.pattern.spring.aop.service..*(..))" id="simplePointcut"/><!-- 配置通知,相當于@Before、@After、@AfterReturn、@Around、@AfterThrowing --><aop:before pointcut-ref="simplePointcut" method="before"/><aop:after pointcut-ref="simplePointcut" method="after"/><aop:after-returning pointcut-ref="simplePointcut" method="afterReturn"/><aop:after-throwing pointcut-ref="simplePointcut" method="afterThrow" throwing="ex"/></aop:aspect></aop:config></beans>?
總結
以上是生活随笔為你收集整理的关于Execution 表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习AOP 之前必须明白的几个概念
- 下一篇: Spring 的前世今生