@Aspect注解使用
启用@AspectJ的支持
@AspectJ
支持可以通过XML或Java风格的配置来启用。在这两种情况下,您还需要确保AspectJ的 aspectjweaver.jar
库在你应用程序的classpath上(AspectJ Weaver版本为1.9或更高版本)。该库可在AspectJ发行版的 lib
目录中找到,也可从Maven Central仓库中找到。
引入aop包之后,实际不需要@EnableAspectJAutoProxy
注解来启用 @AspectJ
支持,默认就是开启的状态
声明一个 Aspect
任何在你的 application context 中定义的bean,该类上有 @Aspect
注解,那么他就会被Spring自动检测到,并用于配置Spring AOP。
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class NotVeryUsefulAspect { }
注意:需要将@Aspect注解的类注入到容器中,可以通过添加@Component来实现
声明一个切点(Pointcut)
@Pointcut是切入点匹配,这个切点声明由两部分组成:标签+切点表达式
支持的标签:
-
execution
:用于匹配方法执行的连接点。这是在使用Spring AOP时要使用的主要切点指定器。 -
within
: 将匹配限制在某些类型内的连接点(使用Spring AOP时,执行在匹配类型内声明的方法)。 -
this
: 将匹配限制在连接点(使用Spring AOP时方法的执行),其中bean引用(Spring AOP代理)是给定类型的实例。 -
target
: 将匹配限制在连接点(使用Spring AOP时方法的执行),其中目标对象(被代理的应用程序对象)是给定类型的实例。 -
args
: 将匹配限制在连接点(使用Spring AOP时方法的执行),其中参数是给定类型的实例。 -
@target
: 限制匹配到连接点(使用Spring AOP时方法的执行),其中执行对象的类有一个给定类型的注解。 -
@args
: 将匹配限制在连接点(使用Spring AOP时方法的执行),其中实际传递的参数的运行时类型有给定类型的注解。 -
@within
: 将匹配限制在具有给定注解的类型中的连接点(使用Spring AOP时,执行在具有给定注解的类型中声明的方法)。 -
@annotation
: 将匹配限制在连接点的主体(Spring AOP中正在运行的方法)具有给定注解的连接点上。 -
bean
:Spring AOP扩展的,AspectJ没有对于指示符,用于匹配特定名称的Bean对象的执行方法。
切点表达式:
例如execution的切点表达式用法:
execution格式为:
execution(modifiers-pattern?
ret-type-pattern
declaring-type-pattern?name-pattern(param-pattern)
throws-pattern?)
-
其中带 ?号的
modifiers-pattern?
,declaring-type-pattern?
,hrows-pattern?
是可选项 -
ret-type-pattern
,name-pattern
,parameters-pattern
是必选项 -
modifier-pattern?
修饰符匹配,如public 表示匹配公有方法 -
ret-type-pattern
返回值类型匹配,* 表示任何返回值 -
declaring-type-pattern?
类路径匹配 -
name-pattern
方法名匹配, *代表所有,set*,代表以set开头的所有方法 -
(param-pattern)
参数匹配,指定方法参数(声明的类型),(…)代表所有参数,(*,String)代表第一个参数为任何值,第二个为String类型,(…,String)代表最后一个参数是String类型 -
throws-pattern?
异常类型匹配
常见的表达式:
- 任何 public 方法的执行
execution(public * *(..))
- 任何名称以
set
开头的方法的执行
execution(* set*(..))
AccountService
接口所定义的任何方法的执行
execution(* com.xyz.service.AccountService.*(..))
- 在
service
包中定义的任何方法的执行
execution(* com.xyz.service.*.*(..))
- 在
service
包或其子包中定义的任何方法的执行
execution(* com.xyz.service..*.*(..))
- service 包内的任何方法的执行
within(com.xyz.service.*)
- 在 service 包或其一个子包中的任何方法的执行
within(com.xyz.service..*)
- 任何代理实现了
AccountService
接口
this(com.xyz.service.AccountService)
- 任何实现
AccountService
接口的目标类
target(com.xyz.service.AccountService)
- 任何方法的参数为一个,且运行时传参类型为
Serializable
args(java.io.Serializable)
- 任何被
@Transactional
注解标注的目标类
@target(org.springframework.transaction.annotation.Transactional)
- 任何被
@Transactional
注解标注的目标类
@within(org.springframework.transaction.annotation.Transactional)
- 任何被
@Transactional
注解标注的方法
@annotation(org.springframework.transaction.annotation.Transactional)
Any join point (method execution only in Spring AOP) which takes a single parameter, and where the runtime type of the argument passed has the @Classified
annotation:
- 方法的参数为一个,且运行时传参被
@Classified
注解标注
@args(com.xyz.security.Classified)
- 任何名字为
tradeService
的 Spring bean
bean(tradeService)
-
Any join point (method execution only in Spring AOP) on Spring beans having names that match the wildcard expression
*Service
: -
任何名字能和通配符
*Service
匹配的 Spring bean
bean(*Service)