循序渐进之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,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...
随机推荐
- python文件操作及os模块常用命令
1.文件打开 文件句柄 = open('文件路径', '模式') 2.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 三种基本 ...
- presto
presto中文站:http://prestodb-china.com/ 进入hadoop机器,进入presto所在bin目录:presto --server localhost:9090 --cat ...
- ios应用版本号设置规则
版本号的格式:v<主版本号>.<副版本号>.<发布号> 版本号的初始值:v1.0.0 管理规则: 主版本号(Major version) 1. 产品的主体构件进行 ...
- java随机生成验证码
package com.yuyuchen.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; im ...
- C语言中一些不被熟知的特性
designated initializers(c99) C99允许你对结构体中指定的变量初始化,如 struct Foo { int x; int y; int z; }; }; 指定初始化也可适用 ...
- NOI 2009 诗人小G
题目描述 Description 小G是一个出色的诗人,经常作诗自娱自乐.但是,他一直被一件事情所困扰,那就是诗的排版问题. 一首诗包含了若干个句子,对于一些连续的短句,可以将它们用空格隔开并放在一行 ...
- package-cleanup
package-cleanup 是一个python开发的命令程序,用来清除本机已安装的.重复的 或孤立的软件包. desktop版的CentOS镜像包含这个工具,而Minimal版的CentOS镜像不 ...
- Spark监控官方文档学习笔记
任务的监控和使用 有几种方式监控spark应用:Web UI,指标和外部方法 Web接口 每个SparkContext都会启动一个web UI,默认是4040端口,用来展示一些信息: 一系列调度的st ...
- 基于编辑距离来判断词语相似度方法(scala版)
词语相似性比较,最容易想到的就是编辑距离,也叫做Levenshtein Distance算法.在Python中是有现成的模块可以帮助做这个的,不过代码也很简单,我这边就用scala实现了一版. 编辑距 ...
- Linux中ls对文件进行按大小排序和按时间排序,设置ls时间格式
1 按文件大小排序 使用 ll -S | grep '^[^d]' // 格式化文件大小形式 ll -Sh | grep '^[^d]' 2 按文件修改时间排序显示 使用 ll -rt 3 设置ls ...