介绍几个 Spring AOP 中的名词:
@Aspect 标记了的类中的方法都可以是连接点再说下通知:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AopTest {
AopTest [] value();
}
先在类上使用 @Aspect 使之成为切面类,然后用 @Component 把切面类加入到 IOC 容器中
@Aspect
@Component
public class AopDemo{
}
@Aspect
@Component
public class AopDemo{
@Pointcut("@annotation(com.xxx.anno.AopTest )")
public void pointcut() {
}
@Before("pointcut()")
public void doBefore(JoinPoint point) {
// do something
}
@After("pointcut()")
public void doAfter() {
// do something
}
}
在需要的方法上加上自定义的注解即可。
单个切面的执行顺序为:Around -> Before -> AfterReturning -> After -> Around;如果存在多个切面,想自定义顺序,可以在切面类上使用 @Order 定义切面顺序。