08 spring AOP开发

本文深入介绍了Spring AOP的两种实现方式:基于XML和注解。从快速入门到切点表达式、通知类型,详细阐述了如何创建目标接口和类,定义切面和增强方法,以及配置织入关系。对于切点表达式,讲解了其书写规则和常见示例。此外,还探讨了如何抽取和复用切点表达式。在基于注解的AOP开发中,强调了使用@Aspect、@通知注解以及启用自动代理的配置步骤。
摘要由CSDN通过智能技术生成

1. 基于XML的AOP开发

1.1 快速入门

① 导入 AOP 相关坐标
② 创建目标接口和目标类(内部有切点)

创建目标接口

public interface TargetInterface {  public void save();  }

创建目标类

public class Target implements TargetInterface {
    public void save() {  System.out.println("running");     }

③ 创建切面类(内部有增强方法)
before()
after()

public class MyAspect {
    public void before(){
        System.out.println("前置增强");
    }
    public void after(){
        System.out.println("后置增强");
    }
}

④ 将目标类和切面类的对象创建权交给 spring

  • 在applicationContext.xml中,配置切面和目标对象
    target就是被增强的方法
    MyAspect就是切面
<!--配置目标对象-->
<bean id="target" class="com.it.aop.Target"></bean>
<!--配置切面-->
<bean id="MyAspect" class="com.it.aop.MyAspect"></bean>

⑤ 在 applicationContext.xml 中配置织入关系

<!--配置织入,告诉spring框架,哪些方法(切点)需要进行哪些增强(前置后置等)-->
<aop:config>
<!--声明切面,告诉spring,MyAspect是切面-->
    <aop:aspect ref="MyAspect">
<!--切面=通知+切点
before method="before"
    第一个before代表是一种通知的类型
    第二个是调用通知的哪个方法(这里是我们切面MyAspect中的Before通知)
pointcut配置切点-->
       <aop:before method="before" pointcut="execution(public void com.it.aop.Target.save())"></aop:before>
    </aop:aspect> 
</aop:config>

⑥ 测试代码

1.2 切点表达式的写法

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
execution(public void com.it.aop.Target.save()

  • 访问修饰符可以省略
  • 返回值类型、包名、类名、方法名可以使用星号* 代表任意
  • 包名与类名之间一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类
  • 参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表

例如:

  • execution(public void com.itheima.aop.Target.method())
    没有返回值 com.it.aop包下的Target类的method方法
  • execution(void com.itheima.aop.Target.*(…))
    没有返回值 com.it.aop包下的Target类的任意方法
  • execution(* com.itheima.aop..(…))
    返回值任意 com.it.aop包下的任意类的任意方法

1.3通知的类型

在这里插入图片描述

1.4 切点表达式的抽取

当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。

<aop:aspect ref="MyAspect">
<!--抽取切点表达式-->
      <aop:pointcut id="myPiontCut" expression="execution(* com.it.aop.*.*(..))"/>
<!--配置切面-->
      <aop:before method="before" pointcut-ref="myPiontCut"></aop:before>
      <aop:after method="after" pointcut-ref="myPiontCut"></aop:after>
</aop:aspect>

2. 基于注解的AOP开发

2.1 步骤

① 导入 AOP 相关坐标
② 创建目标接口和目标类(内部有切点)

@Component("target")
public class Target implements TargetInterface {

    public void save() {
        System.out.println("running");
    }
    
}

③ 创建切面类(内部有增强方法)

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

    //配置前置通知,参数就是切点表达式
    @Before("execution(* com.it.anno.*.*(..))")
    public void before(){
        System.out.println("前置增强");
    }

    public void after(){
        System.out.println("后置增强");
    }
}

④ 将目标类和切面类的对象创建权交给 spring
⑤ 在 applicationContext.xml 中配置织入关系

<!--    开启组件扫描-->
<context:component-scan base-package="com.it.anno"/>
<!--    aop自动代理-->
<aop:aspectj-autoproxy/>

⑥ 测试代码

2.2 注解的通知类型

在这里插入图片描述
当切点表达式都一样时,可以进行抽取

 //定义切点表达式,目标是抽取切点表达式
@Pointcut("execution(* com.it.anno.*.*(..))")
public void  pointcut(){}

//在配置通知时
@Before("MyAspect.pointcut()")
public void before(){
   System.out.println("前置增强");
}

2.3知识要点

2.3.1 注解AOP开发步骤

① 使用@Aspect标注切面类
② 使用@通知注解标注通知方法
③ 在配置文件中配置aop自动代理<aop:aspectj-autoproxy/>(必须有这一句,否则就加载不上注解)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值