在AOP中通知Advice和一个切入点表达式关联
execution用于匹配方法执行的连接点
pointcut
execution: AspectJ与Spring AOP 都支持:
- execution(public **(…)) 切入点为执行所有public方法时
- execution(* set*(..)) 切入点为执行所有set开始的方法时
- execution(* com.xyz.service.AccountService.*(..)) 切入点为执行AccountService类中的所有方法时
- execution(* com.xyz.service..(..)) 切入点为执行com.xyz.service包下的所有方法时
- execution(* com.xyz.service…(. .)) 切入点为执行com.xyz.service包及其子包下的所有方法时
以下只有Spring AOP才支持
- within(com.xyz.service.*) (only in Spring AOP)
- within(com.xyz.service..*) (only in Spring AOP)
within 用于匹配指定类型内的方法执行 - this(com.xyz.service.AccountService) (only in Spring AOP)
this 用于匹配当前AOP代理对象类型的执行方法 - targer(com.xyz.service.AccountService) (only in Spring AOP)
targer 用于匹配当前目标对象类型的执行方法 - args(java.io.Serializable) (only in Spring AOP)
@target(org.springframework.transaction.annotation.Transactional) (only in Spring AOP)
args 用于匹配当前执行的方法传入的参数为指定类型的执行方法 - @within(org.springframework.transaction.annotation.Transactional) (only in Spring AOP)
- @annotation(org.springframework.transaction.annotation.Transactional) (only in Spring AOP)
- @args(com.xyz.security.Classified) (only in Spring AOP)
- bean(tradeService) (only in Spring AOP)
- bean(*Service) (only in Spring AOP)
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="moocAspect" class="com.torey.aop.schema.advice.MoocAspect"></bean>
<bean id="aspectBiz" class="com.torey.aop.schema.advice.biz.AspectBiz"></bean>
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<!--匹配AspectBiz类下的所有方法-->
<aop:pointcut id="aspec" expression="execution(com.torey.aop.schema.advice.biz.AspectBiz.*(..))"/>
<!--匹配以类名以Biz结尾的类的所有方法-->
<!-- <aop:pointcut id="aspec" expression="execution(com.torey.aop.schema.advice.biz.*Biz.*(..))"/>-->
</aop:aspect>
</aop:config>
</beans>