前面描述的几种增强(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. 开启无人驾驶了, 别挡路

最新文章

  1. dfs判断连通图(无向)
  2. download github files
  3. Base64正反编码
  4. Android Handler机制(三)----Looper源码解析
  5. Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
  6. Myeclipse中相同变量高亮显示
  7. Oracle GoldenGate配置异构数据库数据传输(oracle到sqlserer)的dml操作(带pump进程)
  8. JavaScript当页面关闭时向后台发送请求
  9. 重拾java中的 i++ 和 ++i
  10. 6.1、Android Studio的Android Monitor概览
  11. Python内置函数(26)——globals
  12. 『Python CoolBook』数据结构和算法_多变量赋值&amp;“*”的两种用法
  13. python - json/pickle
  14. js将字符串转json
  15. 【Amazon 必考】Amazon Leadership Principles 亚马逊领导力准则
  16. 【动态规划】数字分组I
  17. IOS微信后台运行时候倒计时暂停问题
  18. c#学习路线及目录导航
  19. ASP.NET#JavaScript中调用WebServer注意的问题
  20. Eclipse中使用Maven,报错&ldquo;$MAVEN_HOME&rdquo;

热门文章

  1. 工作随笔——自动重发的凶手--feign
  2. 【python】字典dict
  3. 用keras作CNN卷积网络书本分类(书本、非书本)
  4. php实现socket推送技术
  5. phpexcel导出成绩表
  6. Vue入门总结
  7. lesson - 5 Linux用户和组管理
  8. Spring 自动装配及自动注册的相关配置
  9. Java中静态代码块、构造代码块、构造函数、普通代码块
  10. DBA 优化法则