基于注解的 AOP 开发

1、快速入门

基于注解的 aop 开发步骤:

  1. 创建目标接口和目标类(内部有切点)
  2. 创建切面类(内部有增强方法)
  3. 将目标类和切面类的对象创建权交给spring
  4. 在切面类中使用注解配置织入关系
  5. 在配置文件中开启组件扫描和 AOP 的自动代理
  6. 测试

2、注解配置 AOP 详解

2.1、 注解通知的类型

通知的配置语法:@通知注解("切点表达式")

 接口:

public interface TargetInterface {
    void save();
}
import org.springframework.stereotype.Component;

@Component("target")
public class Target implements TargetInterface {
    @Override
    public void save() {
        System.out.println("save running......");
//        int i = 1/0;
    }
}
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component("myAspect")
@Aspect // 标注当前MyAspect是一个切面类
public class MyAspect {

    //配置前置通知
//    @Before("execution(* com.itheima.anno.*.*(..))")
    public void before() {
        System.out.println("前置增强..........");
    }
//    @AfterReturning("execution(* com.itheima.anno.*.*(..))")
    public void afterReturning() {System.out.println("后置增强..........");}

    // Proceeding JoinPoint: 正在执行的连接点===切点
    @Around("execution(* com.itheima.anno.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("怀绕前增强..........");
        Object proceed = pjp.proceed();// 切点方法
        System.out.println("怀绕后增强..........");
        return proceed;
    }
    public void afterThrowing() {System.out.println("异常抛出增强..........");}

    @After("execution(* com.itheima.anno.*.*(..))")
    public void after() {
        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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--组件扫描-->
    <context:component-scan base-package="com.itheima.anno"/>

    <!--aop 自动代理-->
    <aop:aspectj-autoproxy/>
</beans>
import com.itheima.anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class) // 指定测试引擎
@ContextConfiguration("classpath:applicationContext-anno.xml") // 指定配置文件
public class AnnoTest {
    @Autowired
    private TargetInterface target;

    @Test
    public void test1() {
        target.save();
    }

}

2.2、 切点表达式的抽取

同 xml 配置 aop 一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut

注解定义切点表达式,然后在在增强注解中进行引用。具体如下:

 

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component("myAspect")
@Aspect // 标注当前MyAspect是一个切面类
public class MyAspect {
    // Proceeding JoinPoint: 正在执行的连接点===切点
//    @Around("execution(* com.itheima.anno.*.*(..))")
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("怀绕前增强..........");
        Object proceed = pjp.proceed();// 切点方法
        System.out.println("怀绕后增强..........");
        return proceed;
    }

//    @After("execution(* com.itheima.anno.*.*(..))")
    @After("MyAspect.pointcut()")
    public void after() {
        System.out.println("最终增强..........");
    }

    // 定义切点表达式
    @Pointcut("execution(* com.itheima.anno.*.*(..))")
    public void pointcut(){}
}

3、知识要点

  1. 使用@Aspect标注切面类
  2. 使用@通知注解通知方法
  3. 在配置文件中配置aop自动代理 <aop:aspectj-autoproxy/>
  • 通知注解类型

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值