| 类型 | 说明 |
|---|---|
| arg() | 限制连接点匹配参数为指定类型的执行方法。 |
| @args() | 限制连接点匹配参数由指定注解标注的执行方法。 |
| execution() | 用于匹配的是连接点的执行方法。 |
| this() | 限制连接点匹配AOP代理的Bean引用为指定类型的类。 |
| target() | 限制连接点匹配目标对象为指定类型的类。 |
| @target() | 限制连接点匹配特定的执行对象,这些对象对应的类具备指定类型的注解。 |
| within() | 限制连接点匹配指定的类型。 |
| @within() | 限制连接点匹配指定注解所标注的类型(当使用Spring AOP时,方法定义再由指定的注解所标注的类里)。 |
| @annotation | 限制匹配带有指定注解连接点。 |
|
只有execution指示器是唯一的执行匹配,而其他的指示器都是用于限制匹配的。这说明execution指示器是我们在编写切点定义时最主要的指示器。在此基础上,我们使用其他指示器来限制所匹配的切点。 |


| 类型 | 说明 |
|---|---|
| <aop:advisor> | 定义AOP通知器。 |
| <aop:after> | 定义AOP后置通知(不管被通知的方法是否执行成功) |
| <aop:after-returning> | 定义AOP after-returning通知。 |
| <aop:after-throwing> | 定义 AOP after-throwing 通知。 |
| <aop:around> | 定义 AOP 环绕通知。 |
| <aop:aspect> | 定义切面。 |
| <aop:aspectj-autoproxy> | 启用@AspectJ注解驱动的切面。 |
| <aop:before> | 定义 AOP前置通知。 |
| <aop:config> | 顶层的AOP配置元素。大多数的<aop:*>元素必须包含在<aop:config>元素内。 |
| <aop:declare-parents> | 为被通知的对象引入额外的接口,并透明的实现。 |
| <aop:pointcut> | 定义切点。 |
package com.sjf.bean;/*** 观众类* @author sjf0115**/public class Audience {public void takeSeats(){System.out.println("the audience is taking their seats...");}public void applaud(){System.out.println("very good, clap clap clap...");}public void demandRefund(){System.out.println("very bad, We want our money back...");}}
<bean id = "audience" class = "com.sjf.bean.Audience"></bean>
<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><bean id = "singer" class = "com.sjf.bean.Singer"></bean><bean id = "audience" class = "com.sjf.bean.Audience"></bean><aop:config proxy-target-class="true"><!-- 声明定义一个切面 --><aop:aspect ref = "audience"><!-- 表演之前 --><aop:before method="takeSeats" pointcut="execution(* com.sjf.bean.Singer.perform(..))"/><!-- 表演之后 --><aop:after-returning method="applaud" pointcut="execution(* com.sjf.bean.Singer.perform(..))"/><!-- 表演失败之后 --><aop:after-throwing method="demandRefund" pointcut="execution(* com.sjf.bean.Singer.perform(..))"/></aop:aspect></aop:config></beans>

<aop:config proxy-target-class="true"><!-- 声明定义一个切面 --><aop:aspect ref = "audience"><aop:pointcut id="singerPerfom" expression="execution(* com.sjf.bean.Singer.perform(..))" /><!-- 表演之前 --><aop:before method="takeSeats" pointcut-ref="singerPerfom"/><!-- 表演之后 --><aop:after-returning method="applaud" pointcut-ref="singerPerfom"/><!-- 表演失败之后 --><aop:after-throwing method="demandRefund" pointcut-ref="singerPerfom"/></aop:aspect></aop:config>
public void PerformTime(ProceedingJoinPoint joinPoint){// 演出之前System.out.println("the audience is taking their seats...");try {long start = System.currentTimeMillis();// 执行演出操作joinPoint.proceed();long end = System.currentTimeMillis();// 演出成功System.out.println("very good, clap clap clap...");System.out.println("该演出共需要 "+(end - start) + " milliseconds");} catch (Throwable e) {// 演出失败System.out.println("very bad, We want our money back...");e.printStackTrace();}}
<aop:config proxy-target-class="true"><!-- 声明定义一个切面 --><aop:aspect ref = "audience"><aop:pointcut id="singerPerfom" expression="execution(* com.sjf.bean.Singer.perform(..))" /><!-- 声明环绕通知 --><aop:around method="performTime" pointcut-ref="singerPerfom"/></aop:aspect></aop:config>
|
the audience is taking their seats...
正在上演个人演唱会...
very good, clap clap clap...
该演出共需要 37 milliseconds |
package com.sjf.bean;/*** 歌手实体类* @author sjf0115**/public class Singer {public void perform(String songName) {System.out.println("正在上演个人演唱会,演唱曲目为 " + songName);}}
package com.sjf.bean;/*** 主办方实体类* @author sjf0115**/public class Organizers {public void BeforeSong(String songName){System.out.println("演唱会马上就开始了,演唱歌曲为 " + songName);}public void AfterSong(){System.out.println("演唱曲目结束,谢谢大家...");}}
<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><bean id = "singer" class = "com.sjf.bean.Singer"></bean><bean id = "Organizers" class = "com.sjf.bean.Organizers"></bean><aop:config><!-- 声明定义一个切面 --><aop:aspect ref = "Organizers"><aop:pointcut id="singerPerform" expression="execution(* com.sjf.bean.Singer.perform(String)) and args(song)" /><aop:pointcut id="singerPerform2" expression="execution(* com.sjf.bean.Singer.perform(..))" /><aop:before method="BeforeSong" pointcut-ref="singerPerform" arg-names="song"/><aop:after-returning method="AfterSong" pointcut-ref="singerPerform2"/></aop:aspect></aop:config></beans>

|
演唱会马上就开始了,演唱歌曲为 你是我的眼泪
正在上演个人演唱会,演唱曲目为 你是我的眼泪
演唱曲目结束,谢谢大家... |
原文:http://blog.csdn.net/sunnyyoona/article/details/50655462