前面描述的几种增强(Advice)都是在目标方法范围内织入,而引介(Introduction)不同,直接在类级别上添加目标未实现的接口方法。

spring中可以通过扩展DelegatingIntroductionInterceptor类来实现引介增强类。

下面通过这种方式给一辆普通汽车加上无人驾驶功能

接口Auto

  1. public interface Auto {
  2. void driving();
  3. }

实现类

  1. public class MyCar implements Auto {
  2. @Override
  3. public void driving() {
  4. System.out.println("开车了");
  5. }
  6. }

新建一个接口Intelligent,它具有一个自动驾驶的方法,我们将把这个方法"添加"到MyCar上

  1. public interface Intelligent {
  2. void selfDriving();
  3. }

实现类IntelligentCar,注意,继承了DelegatingIntroductionInterceptor类

  1. public class IntelligentCar extends DelegatingIntroductionInterceptor implements Intelligent {
  2. @Override
  3. public void selfDriving() {
  4. System.out.println("开启无人驾驶了, 别挡路");
  5. }
  6. public Object invoke(MethodInvocation invocation) throws Throwable {
  7. Object obj = super.invoke(invocation);
  8. return obj;
  9. }
  10. }

applicationContext.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/aop
  7. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  8. <bean id="carTarget" class="demo.aop.MyCar" />
  9. <bean id="introduceAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
  10. <constructor-arg>
  11. <bean class="demo.aop.IntelligentCar" />
  12. </constructor-arg>
  13. </bean>
  14. <bean id="myCar" class="org.springframework.aop.framework.ProxyFactoryBean"
  15. p:target-ref="carTarget"
  16. p:interceptorNames="introduceAdvisor"
  17. p:proxyTargetClass="true" />
  18. </beans>

测试代码

  1. public static void main(String[] args) {
  2. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  3. Auto car = (Auto)context.getBean("myCar");
  4. car.driving();
  5. Intelligent intelligentCar = (Intelligent)car;
  6. intelligentCar.selfDriving();
  7. }

运行结果

  1. 开车了
  2. 开启无人驾驶了, 别挡路

循序渐进之Spring AOP(4) - Introduction的更多相关文章

  1. 循序渐进之Spring AOP(3) - 配置代理

    上一篇介绍了几种Advice(增强),并通过代码演示了生成代理的方式,下面来看通过配置文件配置方式把Advice织入目标类. 注意,配置文件方式仍然不是spring AOP的最好方式,学习配置方式也是 ...

  2. 循序渐进之Spring AOP(2) - 基本概念

    学习AOP前要先了解几个重要术语:Joinpoint.Pointcut.Advice 仍然以改装车比喻,拿到心爱的汽车后想做改装,第一件事是什么?找到要改装的地方.车上可改装的地方很多,但每个人感兴趣 ...

  3. Spring AOP之Introduction(@DeclareParents)简介

    Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍: Introductions (known as inter-type declarati ...

  4. Spring AOP之Introduction(@DeclareParents)简介(转)

    Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍: Introductions (known as inter-type declarati ...

  5. 循序渐进之Spring AOP(6) - 使用@Aspect注解

    前面几节的示例看起来让人沮丧,要记忆如此多的接口.类和继承关系,做各种复杂的配置.好在这些只是一种相对过时的实现方式,现在只需要使用@Aspect注解及表达式就可以轻松的使用POJO来定义切面,设计精 ...

  6. 循序渐进之Spring AOP(5) - 创建切面

    在掌握了可用的增强后,接下来要做的就是精确的描述切点.前面的示例都是指定一个目标类并把增强织入到所有方法中,实际开发显然会有更精细的筛选需求,比如对所有类中名称以test结尾的方法加入监控执行时间,或 ...

  7. 循序渐进之Spring AOP(1) - 原理

    AOP全称是Aspect Oriented Programing,通常译为面向切面编程.利用AOP可以对面向对象编程做很好的补充. 用生活中的改装车比喻,工厂用面向对象的方法制造好汽车后,车主往往有些 ...

  8. Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容

    © 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...

  9. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

随机推荐

  1. mysql版本升级

    环境 mysql安装在centos上,需要升级. mysql的版本是 mysql> select version(); +-----------+ | version() | +-------- ...

  2. DotNetCasClient 如何获取Cas服务器返回的attributes中的数据

    最近开始接触做与其它认证系统的集成,其中有个是与某学校的CAS服务器集成.cas服务器认证成功后返回的数据格式如下: 其中红色部分是我需要取出来用于识别用户身份的数据. 一开始,我根据网上的教程,引用 ...

  3. 教程:安装禅道zentao项目管理软件github上的开发版

    该文章转自:吕滔博客 直接从github拉下来的禅道的源码,是跑不起来的.除非你按我的教程来做...哈哈哈(不要脸)~~~~ 禅道官网提供的版本包是带了有安装文件,并有打包合成一些css.js文件的. ...

  4. C# 委托高级应用----线程——创建无阻塞的异步调用(一)

    前言 本文大部分内容来自于mikeperetz的Asynchronous Method Invocation及本人的一些个人体会所得,希望对你有所帮助.原英文文献可以在codeproject中搜索到. ...

  5. hibernate核心API使用

    1.Configuration 加载核心配置文件,核心配置文件名称和位置固定,否则会找不到 2.SessionFactory对象一个项目只创建一个,大家公用 根据配置文件和映射关系创建表 由于要创建表 ...

  6. ArcGIS API for JavaScript 4.2学习笔记[29] 热点(密度)分析——以报警频率为例【使用Geoprocessor类】

    这个就颇有插值分析的样子了.也可以说是密度分析.做出来就是一个热力地图的样子. 比如,人口密度,降雨分布等.这都可以由这个例子做出来类似的. 由于上一篇已经介绍过Geoprocessor类和Param ...

  7. SpringMVC对包的扫描范围扩大后,导致的事务配置不生效问题

    问题场景 项目使用的框架:Spring 4.1.4 + Hibernate 4.3.8 + MySQL. web.xml中对Spring的配置: <!-- 把 Spring 容器集成到 Web ...

  8. 详细的DedeCMS(织梦)目录权限安全设置教程

    一.目录权限根据统计,绝大部分网站的攻击都在根目录开始的,因此,栏目目录不能设置在根目录.DEDECMS部署完成后,重点目录设置如下:1)将install删除.2) data.templets.upl ...

  9. Q:java中serialVersionUID的作用

    @转载自:http://www.cnblogs.com/guanghuiqq/archive/2012/07/18/2597036.html   简单来说,Java的序列化机制是通过在运行时判断类的s ...

  10. vuex 基本用法、兄弟组件通信,参数传递

    vuex主要是是做数据交互,父子组件传值可以很容易办到,但是兄弟组件间传值,需要先将值传给父组件,再传给子组件,异常麻烦. vuex大概思路:a=new Vue(),发射数据'msg':a.$emit ...