1. Spring中AspectJ指示器
execution(* concert.Performance.perform(..)) && within(concert.*)
* :表示任意返回值。
- arg() 限制连接点匹配参数为指定类型的的执行方法。
- @args() 限制连接点匹配参数由指定注解标注的执行方法。
- execution() 用于匹配是连接点的执行方法。
- this() 限制连接点匹配AOP代理的bean引用为指定类型的类。
- target 限制连接点匹配目标对象为指定类型的类。
- @target() 限制连接点匹配特定的执行对象,这些对象对应的类要具有指定类型的注解
- within() 限制连接点匹配制定的类型。
- within() 限制连接点匹配指定注解所标注的类型。
- @annotation 限定匹配带有指定注解的连接点。
execution(* concert.Performance.perform(..)) && within(concert.*)
* :表示任意返回值。
(..) :表示任意参数。
注解使用:&&/||/!
xml使用:and/or/not
使用bean:execution(* concert.Performance.perform()) and bean('beanId') / execution(* concert.Performance.perform()) and !bean('beanId')
3. 通知方法
@After/@AfterReturning/@AfterThrowing/@Around/@Before
4. 切面
@Aspect
public class Audience {
@Before("execution(* *.source.aop.TestBean.test(..))")
public void silenceCellPhones(){
System.out.println("Silence cell phones");
}
}
@Aspect
public class AudienceCut {
@Pointcut("execution(* *.source.aop.TestBean.test(..))")
public void test(){}
@Before("test()")
public void silenceCellPhones(){
System.out.println("Silence cell phones");
}
}
@Aspect
public class AudienceAround {
@Pointcut("execution(* *.source.aop.TestBean.test(..))")
public void test(){}
public void watchPerformance(ProceedingJoinPoint p){
try {
System.out.println("前置");
p.proceed();
System.out.println("后置");
}catch (Throwable throwable){
throwable.printStackTrace();
}
}
}
@Aspect
public class AudienceParam {
@Pointcut("execution(* *.source.aop.TestBean.param(String)) && args(name)")
public void param(String name){}
@Before("param(name)")
public void silenceCellPhones(String name){
System.out.println(name+":Silence cell phones");
}
}
5. 启动aspectJ自动代理
@EnableAspectJAutoProxy/<aop:aspectj-autoproxy/>
6.扩展新接口
@Aspect
public class EncoreableIntroducer {
@DeclareParents(value = "com.source.aop.TestBean+",defaultImpl = DefaultEncoreable.class)
public static Encoreable encoreable;
}
Encoreable encoreable = (Encoreable)context.getBean("testBean",TestBean.class);
encoreable.performEncore();
XML方式配置
<bean id="test" class="com.source.aop.TestBean"/>
<bean id="audience" class="com.source.aop.Audience"/>
<bean id="audienceAround" class="com.source.aop.AudienceAround"/>
<bean id="audienceParam" class="com.source.aop.AudienceParam"/>
<!-
<aop:config>//没有切点类型
<aop:aspect ref="audience">
<aop:before pointcut="execution(* com.source.aop.TestBean.test(..))" method="silenceCellPhones"/>
<aop:before pointcut="execution(* com.source.aop.TestBean.test(..))" method="takeSeats"/>
<aop:after pointcut="execution(* com.source.aop.TestBean.test(..))" method="applause"/>
<aop:after pointcut="execution(* com.source.aop.TestBean.test(..))" method="demandRefund"/>
</aop:aspect>
</aop:config>
-->
<aop:config>
<!--
<aop:aspect ref="audience">//有切点
<aop:pointcut id="pointTest" expression="execution(* com.source.aop.TestBean.test(..))"/>
<aop:before pointcut-ref="pointTest" method="silenceCellPhones"/>
<aop:before pointcut-ref="pointTest" method="takeSeats"/>
<aop:after pointcut-ref="pointTest" method="applause"/>
<aop:after pointcut-ref="pointTest" method="demandRefund"/>
</aop:aspect>
<aop:aspect ref="audienceAround">//环绕增强
<aop:pointcut id="pointTest" expression="execution(* com.source.aop.TestBean.test(..))"/>
<aop:around method="watchPerformance" pointcut-ref="pointTest"/>
</aop:aspect>
<aop:aspect ref="audienceParam">//处理参数
<aop:pointcut id="pointParam" expression="execution(* com.source.aop.TestBean.param(String)) and args(name)"/>
<aop:before method="demandRefund" pointcut-ref="pointParam"/>
</aop:aspect>
-->
<aop:aspect>//添加新功能
<aop:declare-parents
types-matching="com.source.aop.TestBean+"
implement-interface="com.source.aop.Encoreable"
default-impl="com.source.aop.DefaultEncoreable"/>
</aop:aspect>
</aop:config>