基于 XML 的 AOP 开发

1 快速入门

  1. 导入 AOP 相关坐标
  2. 创建目标接口和目标类(内部有切点)
  3. 创建切面类(内部有增强方法)
  4. 将目标类和切面类的对象创建权交给 spring
  5. 在 applicationContext.xml 中配置织入关系
  6. 测试代码

2 XML 配置 AOP详解

2.1、切点表达式的写法

表达式语法:

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

2.2. 通知的类型

通知的配置语法:

 

public interface TargetInterface {
    void save();
}
public class Target implements TargetInterface {
    @Override
    public void save() {
        System.out.println("save running......");
//        int i = 1/0;
    }
}
import com.itheima.aop.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.xml") // 指定配置文件
public class AopTest {
    @Autowired
    private TargetInterface target;

    @Test
    public void test1() {
        target.save();
    }
}
import org.aspectj.lang.ProceedingJoinPoint;

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

    // Proceeding JoinPoint: 正在执行的连接点===切点
    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("异常抛出增强..........");
    }
    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"
       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="target" class="com.itheima.aop.Target"></bean>

    <!--切面对象-->
    <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>

    <!--配置织人:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--切面:切点+通知-->
<!--            <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.save())"></aop:before>-->
            <!--<aop:before method="before" pointcut="execution(* com.itheima.aop.*.*(..))"></aop:before>
            <aop:after-returning method="afterReturning" pointcut="execution(* com.itheima.aop.*.*(..))"></aop:after-returning>-->
<!--            <aop:around method="around" pointcut="execution(* com.itheima.aop.*.*(..))"/>-->
<!--            <aop:after-throwing method="afterThrowing" pointcut="execution(* com.itheima.aop.*.*(..))"/>-->
            <aop:after method="after" pointcut="execution(* com.itheima.aop.*.*(..))"/>
        </aop:aspect>
    </aop:config>
</beans>

2.3、切点表达式的抽取

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

抽取后的切点表达式

   <!--配置织人:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--抽取切点表达式-->
            <aop:pointcut id="myPoincout" expression="execution(* com.itheima.aop.*.*(..))"></aop:pointcut>

            <aop:around method="around" pointcut-ref="myPoincout"/>
            <aop:after method="after" pointcut-ref="myPoincout"/>
        </aop:aspect>
    </aop:config>

3、知识要点

  • aop 织入的配置

 

  • 通知的类型:前置通知、后置通知、环绕通知、异常抛出通知、最终通知
  • 切点表达式的写法:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值