AOP -连接点和切点的区别

6 篇文章 0 订阅
2 篇文章 0 订阅

一直没搞懂连接点和切点的关系区别,《spring 实战》这本书第四章有介绍,但是还是没看懂。所以查了点资料,感觉还是通过代码看最直观。

1. 创建一个简单的aspect切面class

public class Logging {/**

    * This is the method which I would like to execute

    * before a selected method execution.

    */public void beforeAdvice() {

        System.out.println("Going to setup student profile.");

    }

    /**

    * This is the method which I would like to execute

    * after a selected method execution.

    */public void afterAdvice() {

        System.out.println("Student profile has been setup.");

}

    /**

    * This is the method which I would like to execute

    * when any method returns.

    */

    public void afterReturningAdvice(Object retVal) {

        System.out.println("Returning:" + retVal.toString());

    }

    /**

    * This is the method which I would like to execute

    * if there is an exception raised.

    */

    public void AfterThrowingAdvice(IllegalArgumentException ex) {

        System.out.println("There has been an exception: " + ex.toString());

    }

}

2. SpringAOP.xml配置

<bean id="student" class="com.seeyon.SpringBean.aop.Student" p:name="yangyu" p:age="27"></bean>

<bean id="logging" class="com.seeyon.SpringBean.aop.Logging"></bean>

<aop:config>

<aop:aspect id="log" ref="logging"> ------切面class

<aop:pointcut id="studentMethod" expression="execution(* com.seeyon.SpringBean.aop.Student.get*(..))"/> -------切点

<aop:before pointcut-ref="studentMethod" method="beforeAdvice"/>------方法执行之前触发切面class的beforeAdvice方法

<aop:after pointcut-ref="studentMethod" method="afterAdvice"/>------方法执行之后触发切面class的afterAdvice方法

</aop:aspect>

</aop:config>

切点:"execution(* com.seeyon.SpringBean.aop.Student.get*(..))"

  1. 第一个*代表方法的返回值是任意的
  2. get*代表以get开头的所有方法
  3. (..)代表方法的参数是任意个数

3. 测试类

public class test {

    public static void main(String[] args) {

        ApplicationContext context =

                new ClassPathXmlApplicationContext("SpringAop.xml");

        Student student = (Student) context.getBean("student");

        student.getName();

//        student.getAge();

//        student.printThrowException();

    }

}

/**
*执行结果
*Going to setup student profile.
*
*Name : yangyu
*
*Student profile has been setup.
*/

4. 根据SpringAOP.xml配置

  1. 切点扫描到 getName()这个方法被调用时 这个连接点
  2. 在getName()被调用之前触发切面class的beforeAdvice方法,打印出(Going to setup student profile.)
  3. 执行getName()
  4. 方法执行之后触发切面class的afterAdvice方法打印(Student profile has been setup.)

5. 结论

  • Aspect(切面):上面xml中aop:aspect标签表示的就是一个切面;
  • Pointcut (切点):aop:pointcut标签表示一个切点,也就是满足条件被扫描到的目标方法;
  • Target Object(目标对象): 包含连接点的对象。也被称作被通知或被代理对象。(如上面所说的目标方法)
  • Join Point(连接点):连接点是一个虚拟的概念,可以理解为所有满足切点扫描条件的所有的时机。method="beforeAdvice"表示被扫描到目标方法后确定增强的时机,也就是连接点,这个表示前置一个增强(这里翻译Advice更好一点,而不是通知)
  • Advice(增强):增强是一个具体的函数,例如,日志记录,权限验证,事务控制,性能检测,错误信息检测等。
  • Introduction(引入): 添加方法或字段到被增强的类,Spring允许引入新的接口到任何被增强的对象。例如,你可以使用一个引入使任何对象实现 IsModified接口,来简化缓存。Spring中要使用Introduction, 可有通过DelegatingIntroductionInterceptor来实现增强,通过DefaultIntroductionAdvisor来配置Advice和代理类要实现的接口
  • Weaving(织入):组装切面来创建一个被通知对象。这可以在编译时完成(例如使用AspectJ编译器),也可以在运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入。等候时机(连接点)
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值