循序渐进之Spring AOP(4) - Introduction
前面描述的几种增强(Advice)都是在目标方法范围内织入,而引介(Introduction)不同,直接在类级别上添加目标未实现的接口方法。
在spring中可以通过扩展DelegatingIntroductionInterceptor类来实现引介增强类。
下面通过这种方式给一辆普通汽车加上无人驾驶功能
接口Auto
- public interface Auto {
- void driving();
- }
实现类
- public class MyCar implements Auto {
- @Override
- public void driving() {
- System.out.println("开车了");
- }
- }
新建一个接口Intelligent,它具有一个自动驾驶的方法,我们将把这个方法"添加"到MyCar上
- public interface Intelligent {
- void selfDriving();
- }
实现类IntelligentCar,注意,继承了DelegatingIntroductionInterceptor类
- public class IntelligentCar extends DelegatingIntroductionInterceptor implements Intelligent {
- @Override
- public void selfDriving() {
- System.out.println("开启无人驾驶了, 别挡路");
- }
- public Object invoke(MethodInvocation invocation) throws Throwable {
- Object obj = super.invoke(invocation);
- return obj;
- }
- }
applicationContext.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
- <bean id="carTarget" class="demo.aop.MyCar" />
- <bean id="introduceAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
- <constructor-arg>
- <bean class="demo.aop.IntelligentCar" />
- </constructor-arg>
- </bean>
- <bean id="myCar" class="org.springframework.aop.framework.ProxyFactoryBean"
- p:target-ref="carTarget"
- p:interceptorNames="introduceAdvisor"
- p:proxyTargetClass="true" />
- </beans>
测试代码
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- Auto car = (Auto)context.getBean("myCar");
- car.driving();
- Intelligent intelligentCar = (Intelligent)car;
- intelligentCar.selfDriving();
- }
运行结果
- 开车了
- 开启无人驾驶了, 别挡路
循序渐进之Spring AOP(4) - Introduction的更多相关文章
- 循序渐进之Spring AOP(3) - 配置代理
上一篇介绍了几种Advice(增强),并通过代码演示了生成代理的方式,下面来看通过配置文件配置方式把Advice织入目标类. 注意,配置文件方式仍然不是spring AOP的最好方式,学习配置方式也是 ...
- 循序渐进之Spring AOP(2) - 基本概念
学习AOP前要先了解几个重要术语:Joinpoint.Pointcut.Advice 仍然以改装车比喻,拿到心爱的汽车后想做改装,第一件事是什么?找到要改装的地方.车上可改装的地方很多,但每个人感兴趣 ...
- Spring AOP之Introduction(@DeclareParents)简介
Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍: Introductions (known as inter-type declarati ...
- Spring AOP之Introduction(@DeclareParents)简介(转)
Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍: Introductions (known as inter-type declarati ...
- 循序渐进之Spring AOP(6) - 使用@Aspect注解
前面几节的示例看起来让人沮丧,要记忆如此多的接口.类和继承关系,做各种复杂的配置.好在这些只是一种相对过时的实现方式,现在只需要使用@Aspect注解及表达式就可以轻松的使用POJO来定义切面,设计精 ...
- 循序渐进之Spring AOP(5) - 创建切面
在掌握了可用的增强后,接下来要做的就是精确的描述切点.前面的示例都是指定一个目标类并把增强织入到所有方法中,实际开发显然会有更精细的筛选需求,比如对所有类中名称以test结尾的方法加入监控执行时间,或 ...
- 循序渐进之Spring AOP(1) - 原理
AOP全称是Aspect Oriented Programing,通常译为面向切面编程.利用AOP可以对面向对象编程做很好的补充. 用生活中的改装车比喻,工厂用面向对象的方法制造好汽车后,车主往往有些 ...
- Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容
© 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...
- 学习AOP之认识一下Spring AOP
心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...
随机推荐
- std::shared_ptr<void>的工作原理
前戏 先抛出两个问题 如果delete一个指针,但是它真实的类型和指针类型不一样会发生什么? 是谁调用了析构函数? 下面这段代码会发生什么有趣的事情? // delete_diff_type.cpp ...
- iOS 模拟器安装应用
iOS模拟器是苹果Xcode IDE的一部分,主要用来为Mac,iPhone和iPad创建应用程序,为了给iOS模拟器打包应用程序,利用–package 在命令行上执行ADT并使用–target来指定 ...
- 推荐:让你快速搞定各服务端(api,pc,mobile,wechat)代码
如果你在写服务端 (PHP) ,会因为项目须求(做app.pc.mobiel.微信) 而写几套代码的,你不觉得很累吗? 现在的很多开源框架商用版本在做程序方面都是这么一套一套的,维护起来,二开起来特别 ...
- 【NOIP模拟】【USACO】 Bovine Genomics
Description 给定两个字符串集合A,B,均包含N个字符串,长度均为M,求一个最短的区间[l,r],使得不存在字符串\(a\in A,b\in B,\)且\(a[l,r]=b[l,r]\) , ...
- 聊天机器人(基于android)
1.本人最近写了一个小项目关于语音聊天的,采用讯飞语音引擎和数据,看看效果 2.项目名称叫小秘书,它可以和你进行交互,可以通过语音聊天,蛮有意思的,聊天内容你也可以定制 3.如果想做这款应用,先看看我 ...
- Ubuntu14.04下安装 boost (boost_1.54 最简单的方法)
直接执行命令: sudo apt-get install libboost-dev 测试: 创建一个 boost_test.cpp 文件,写入 #include<iostream> #i ...
- Android 再按一次退出程序三种办法
在Xamarin android中双击返回键退出程序的第一种做法 思路就是当用户按下返回键的时间超过两秒就退出,根据Keycode.Back判断用户按下的是返回键,重写这个OnKeyDown Date ...
- Thinkphp环境搭建
一.准备工作-基础知识 1.php环境配置 配置php文件运行环境Apache,phpstudy集成环境(还有别的集成环境都可以). 2.设计数据库 根据需求设计table,可以用一些数据库管理工具n ...
- 转载|chrome developer tool—— 断点调试篇
断点,调试器的功能之一,可以让程序中断在需要的地方,从而方便其分析.也可以在一次调试中设置断点,下一次只需让程序自动运行到设置断点位置,便可在上次设置断点的位置中断下来,极大的方便了操作,同时节省了时 ...
- 关于文件上传的ajax交互
首先我们来了解一下上传文件 <input type="file"> input的file常用上传类型 后缀名 MIME名称 *.3gpp audio/3gpp, vid ...