Introduction的Advice的实现Introduction可以在不改动目标类定义的情况下,为目标类增加新的属性和行为。在Spring中,为目标对象添加新的属性和行为必须声明相应的接口以及相应的实现。这样,再通过特定的拦截器将新的接口定义以及实现类中的逻辑附加到目标对象上。之后,目标对象(确切的说,是目标对象的代理对象)就拥有了新的状态和行为。这个特定的拦截器就是org.springframework.aop.IntroductionInterceptor,其定义如下public interface IntroductionInterception extends MethodInterceptor,DynamicIntroductionAdvice{}public interface DynamicIntroductionAdvice extends Advice{boolean implementsInterface(Class intf)}IntroductionInterceptor继承了MethodInterceptor以及DynamicIntroductionAdvice。通过DynamicIntroductionAdvice,可以界定当前的IntroductionInterceptor为哪些接口提供相应的拦截功能。通过MethodInterceptor,IntroductionInterceptor 就可以处理新添加的接口上的方法调用了如果把每个目标对象实例看作盒装牛奶生产线上的那一盒盒牛奶的话,那么生产合格证就是新的Introduction逻辑,而introductionInterceptor
就是把这些生产合格证贴到一盒盒牛奶上的那个人。要对目标对象进行拦截并添加Introduction的逻辑,我们可以直接扩展IntroductionInterceptor,然后在子类的invoke方法中实现所有的拦截逻辑
除非特殊情况下需要直接扩展IntroductionInterceptor,大多数时候,直接使用Spring提供的两个现成的实现类就可以了.DelegatingIntroductionInterceptor 和 DelegatePerTargetObjectIntroductionInterceptor  下面是我实现Introduciton的具体例子首先给出ISomeBean接口及其实现类public interface ISomeBean {public void doSth();
}public class ISomeBeanImpl implements ISomeBean{@Overridepublic void doSth() {// TODO Auto-generated method stubSystem.out.println("do something");}
}使用IntroductionInterceptor为ISomeBean添加新的状态或者行为,我们可以按照如下的步骤进行
(1)为新的状态和行为定义接口public interface IOtherBean {public void doOther();
}下面同时实现了IntroductionInterceptor,其实我还不是很明白为什么写成这样
public class SomeBeanIntroductionInterceptor implements IOtherBean,IntroductionInterceptor{/*** 判断调用的方法是否为指定类中的方法* 如果Method代表了一个方法 那么调用它的invoke就相当于执行了它代表的这个方法*/@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {if(implementsInterface(invocation.getMethod().getDeclaringClass())){System.out.println("zhangsi");return invocation.getMethod().invoke(this, invocation.getArguments());}return invocation.proceed();}/*** 判断clazz是否为给定接口IOtherBean的实现*/@Overridepublic boolean implementsInterface(Class clazz) {return clazz.isAssignableFrom(IOtherBean.class);}@Overridepublic void doOther() {System.out.println("do other thing");}
}配置文件定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 目标对象 --><bean id="someBeanTarget" class="com.spring.targetImpl.ISomeBeanImpl" /><!-- 通知 --><bean id="someBeanAdvice" class="aop.spring.advice.SomeBeanIntroductionInterceptor"></bean><!-- 通知者,只能以构造器方法注入 --><!-- DefaultIntroductionAdvisor(Advice advice, IntroductionInfo introductionInfo) --><!-- introductionInfo - the IntroductionInfo that describes the interface to introduce (may be null) --><bean id="introductionAdvisor"class="org.springframework.aop.support.DefaultIntroductionAdvisor"><constructor-arg ref="someBeanAdvice" /><constructor-arg value="aop.spring.target.IOtherBean" /></bean><!-- 代理,将我们的切面织入到目标对象 --><bean id="someBeanProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 若目标对象实现了代理接口,则可以提供代理接口的配置 --><property name="proxyInterfaces" value="aop.spring.target.ISomeBean" /><!-- 配置目标对象 --><property name="target" ref="someBeanTarget" /><!-- 配置切面 --><property name="interceptorNames"><list><value>introductionAdvisor</value></list></property></bean>
</beans>测试类如下:
public class AdviceTest {@Testpublic void test() {AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("/beans.xml");/*** 将代理对象强制转换为目标对象的具体类型*/ISomeBean weaver=(ISomeBean) ctx.getBean("someBeanProxy");weaver.doSth();System.out.println("=====");((IOtherBean)weaver).doOther();}
}
我理解的是获得weaver对象同时具备了ISomeBean和IOtherBean的属性,所以可以被当做这两个对象来使用。
这就是Introduction的典型应用。就是有一点不明白的是为什么IOtherBean的实现类要同时实现IntroductionInterceptor
如果分开的该怎么实现呢?

Introduction的Advice的实现相关推荐

  1. Spring 中的Advice类型介绍

    Spring 中的 Advice 类型介绍 翻译原文链接 Introduction to Advice Types in Spring 1. 概述 在本文中,我们将讨论可以在 Spring 中创建的不 ...

  2. 最新最全面的Spring详解(四)——面向切面编程

    前言 本文为 [Spring]面向切面编程 相关知识,下边将对AOP概述(包含什么是AOP.AOP相关术语.Spring AOP通知类型),Spring AOP能力和目标,AOP代理,@AspectJ ...

  3. Spring AOP基础组件 Advisor

    相关阅读 Spring AOP基础组件 Pointcut Spring AOP基础组件 Advice 简介 持有AOP Advice和决定Advice适用性的过滤器(比如Pointcut)的基础接口: ...

  4. Java - 你如何理解AOP中的连接点(Joinpoint)、切点(Pointcut)、增强(Advice)、引介(Introduction)、织入(Weaving)、切面(Aspect)这些概念?

    分享一个大牛的人工智能教程.零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net a. 连接点(Joinpoint):程序执行的某个特 ...

  5. Advice for students of machine learning--转

    原文地址:http://www.mimno.org/articles/ml-learn/ written by david mimno One of my students recently aske ...

  6. Introduction for i-Teams

    Introduction for i-Teams Jan 17 文章目录 Introduction for i-Teams Online platform Covid Zoom rooms Amy W ...

  7. Spring中的Advice类型及其应用

    http://blog.csdn.net/myyate/article/details/1822150 在Spring中,Advice都是通过Interceptor来实现的,主要有以下几种: 1. 环 ...

  8. Practical Go: Real world advice for writing maintainable Go programs

    转载地址:Practical Go: Real world advice for writing maintainable Go programs Table of Contents Introduc ...

  9. Spring AOP:搞清楚advice的执行顺序

    文章目录 目录 AOP的核心概念 模拟aspect advice的执行过程 同一aspect,不同advice的执行顺序 不同aspect,advice的执行顺序 同一aspect,相同advice的 ...

  10. Spring关于AOP中关于advice的执行顺序

    AOP的核心概念 要完全理解Spring AOP首先要理解AOP的核心概念和术语,这些术语并不是Spring指定的,而且很不幸,这些术语并不能直观理解,但是,如果Spring使用自己的术语,那将更加令 ...

最新文章

  1. 日常生活小技巧 -- markdown编辑器
  2. 通用函数get和set
  3. SAP Fiori issue排查:why search by ID does not work
  4. 完美国际真数苹果_章子怡玩出新花样,雷人造型别有韵味!和小苹果同框犹如亲姐妹...
  5. vs2017编译QT with ssl
  6. block介绍(四)揭开神秘面纱(下)
  7. jqGrid 使用案例及笔记
  8. 使用jmeter 上传文件
  9. 从零开始学习jQuery-------jQuery元素选择器(三)
  10. IOS开发基础知识--碎片33
  11. c语言上机填空改错试题,2013年计算机二级C语言上机试题六十二及答案
  12. n维椭球体积公式_【栗子资料】高中生必看,高中年级所有数学公式大全
  13. QAM的符号能量及比特能量
  14. 华为云服务器建站教程
  15. 基于wavesurfer,regions 封装的可视化音标标注控件
  16. mysql从创库到查询基本命令
  17. mysql latch和缓存关系_latch:cachebufferschains等待事件导致的latch争用的原理原因与...
  18. 图像处理之K-Means算法演示
  19. 第八期杭州NodeParty x Rokid技术分享会回顾
  20. dubbo的常用容错机制

热门文章

  1. 数据可视化与大数据分析
  2. odoo 12: 字段(Fields)
  3. 12、Urban Radiance Fields
  4. 服务器对接qq微信聊天,java仿QQ微信聊天室功能的实现
  5. win查看服务器主板型号,Win10怎么看电脑主板型号?
  6. css选择器尽量简短_网络视频格式,可帮助您选择的简短指南
  7. android Rect
  8. 高考作文《细雨闲花》
  9. 怎样给WordPress友情链接添加nofollow
  10. 28岁以后,我再也没为工作拼过命