• 简介允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象
  • 由<aop:aspect>中的<aop:declare-parents>元素声明该元素用于声明所匹配的类型拥有一个新的parents(因此得名)

配置:

  1. <aop:aspect id="usageTrackerAspect" ref="usageTracking">
  2. <aop:declare-parents types-matching="com.xyz.myapp.service.*+"
  3. implement-interface="com.xyz.myapp.service.tracking.UsageTracked"
  4. default-impl="com.xyz.myapp.service.tracking.DefaultUsageTracked"/>
  5. </aop:aspect>
  1. public void recordUsage(){
  2. usageTracked.incrementUseCount();
  3. }
  1. UsageTracked usageTracked = (UsageTracked) context.getBean("myservice");

例子:

新建接口Fit及实现类FitImpl

  1. package com.aop.schema;
  2.  
  3. public interface Fit {
  4. void filter();
  5. }
  1. package com.aop.schema;
  2.  
  3. public class FitImpl implements Fit {
  4.  
  5. @Override
  6. public void filter() {
  7. System.out.println("FitImpl.filter");
  8.  
  9. }
  10.  
  11. }

切面类(同上节,不做修改了):

  1. package com.aop.schema.advice;
  2.  
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4.  
  5. /**
  6. *
  7. * 切面类
  8. *
  9. */
  10. public class MyAspect {
  11.  
  12. public void before(){
  13. System.out.println("MyAspect.before");
  14. }
  15.  
  16. public void afterreturning(){
  17. System.out.println("MyAspect.afterreturning");
  18. }
  19.  
  20. public void afterthrowing(){
  21. System.out.println("MyAspect.afterthrowing");
  22. }
  23.  
  24. public void after(){
  25. System.out.println("MyAspect.after");
  26. }
  27.  
  28. public void around(ProceedingJoinPoint pjp) {
  29. try {
  30. System.out.println("MyAspect.around_1");
  31. Object obj=pjp.proceed();
  32. System.out.println("MyAspect.around_2");
  33. } catch (Throwable e) {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. public void around_init(ProceedingJoinPoint pjp,String name,int age) {
  39. System.out.println(name+" "+age);
  40. try {
  41. System.out.println("MyAspect.around_1");
  42. Object obj=pjp.proceed();
  43. System.out.println("MyAspect.around_2");
  44. } catch (Throwable e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }

XML配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.1.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
  12.  
  13. <bean id="myAspect" class="com.aop.schema.advice.MyAspect"></bean>
  14. <aop:config>
  15. <aop:aspect id="myAspectAOP" ref="myAspect">
  16. <!-- com.aop.schema.advice.*+ 指的是advice包下的所有类 -->
  17. <aop:declare-parents types-matching="com.aop.schema.advice.*+"
  18. implement-interface="com.aop.schema.Fit"
  19. default-impl="com.aop.schema.FitImpl"/>
  20. </aop:aspect>
  21. </aop:config>
  22.  
  23. </beans>

单元测试:

  1. package com.aop.schema;
  2.  
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7. import com.aop.schema.advice.ApsectBiz;
  8.  
  9. public class UnitTest {
  10.  
  11. @Test
  12. public void test(){
  13. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-aop.xml");
  14. Fit fit = (Fit)context.getBean("myAspect");
  15. fit.filter();
  16. }
  17. }

测试结果:

  1. 七月 11, 2015 1:15:13 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  2. INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@31b3c607: startup date [Sat Jul 11 13:15:13 CST 2015]; root of context hierarchy
  3. 七月 11, 2015 1:15:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. INFO: Loading XML bean definitions from class path resource [spring-aop.xml]
  5. FitImpl.filter

PS:说明为指定的类型强制指定了一个父类。

 

Spring学习(23)--- AOP之Introductions应用的更多相关文章

  1. Spring学习之AOP的实现方式

    Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...

  2. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  3. spring学习(二) ———— AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

  4. Spring 学习二-----AOP的原理与简单实践

    一.Spring  AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...

  5. Spring学习之AOP

    Spring-AOP(Aspect-orented programming) 在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutt ...

  6. Spring学习之Aop的各种增强方法

    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...

  7. Spring学习之Aop的基本概念

    转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...

  8. Spring学习之AOP与事务

      一.概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续, ...

  9. spring学习笔记-AOP

    1.aop:aspect oriented programming 面向切面编程 2.aop在spring中的作用:   提供声明式服务(声明式事务) 允许用户实现自定义切面 3.aop:在不改变原有 ...

  10. Spring 学习之AOP

    1. 走进面前切面编程 编程范式: 面向过程编程,c语言: 面向对象编程:c++,java,c#; 函数式编程: 事件驱动编程: 面向切面编程: AOP是一种编程范式,不是编程语言:解决特定问题,不能 ...

随机推荐

  1. 学习Java之前操作环境的安装及配置

    1.根据自己的系统版本下载相应版本的JDK(Java开发运行时环境) 查看自己系统版本的方法:在桌面上右键计算机(win7,win10是此电脑,XP是我的电脑),点击属性,进入到计算机属性页面以后里面 ...

  2. Python 操作 MySQL 的正确姿势

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:邵建永 使用Python进行MySQL的库主要有三个,Python-MySQL(更熟悉的名字可能是MyS ...

  3. AIX误删除LV后如何进行现场保护和数据恢复工作

    在AIX环境下,若因维护误操作.存储mapping错误等,不小心将LV误删除,这种损失通常是巨大的.删除后的不当保护及恢复操作可能使数据无法恢复,也可能增加处理的时间与算法复杂度.如何有效保护现场,并 ...

  4. 悬挂else引发的问题

    这个问题虽然已经为人熟知,而且也并非C语言所独有,但即使是有多年经验的C程序员也常常在此失误过. 考虑下面的程序片段: if (x == 0) if (y == 0) error(); else{ z ...

  5. JQuery速成大法

    什么是JQuery呢,很多都是只闻其名. jQuery是一个快速.简洁的JavaScript框架,是一个优秀的JavaScript代码库.jQuery设计的宗旨是"write Less,Do ...

  6. [UWP]了解模板化控件(8):ItemsControl

    1. 模仿ItemsControl 顾名思义,ItemsControl是展示一组数据的控件,它是UWP UI系统中最重要的控件之一,和展示单一数据的ContentControl构成了UWP UI的绝大 ...

  7. 《JavaScript面向对象编程指南(第2版)》读书笔记(二)

    <JavaScript面向对象编程指南(第2版)>读书笔记(一) <JavaScript面向对象编程指南(第2版)>读书笔记(二) 目录 一.基本类型 1.1 字符串 1.2 ...

  8. Android 安卓实现页面相互跳转并相互传递参数

    一.对于两个页面之间相互传值,跳转的时候我们使用 startActivityForResult(intent,0),而不是startActivity(intent) 这个方法 第一个页面中在触发跳转的 ...

  9. 2016年BAT公司常见的Web前端面试题整理

    1.JavaScript是一门什么样的语言,它有哪些特点? 没有标准答案. 2.JavaScript的数据类型都有什么? 基本数据类型:String,boolean,Number,Undefined ...

  10. NancyFx 2.0的开源框架的使用-Stateless(二)

    继续上一篇Stateless的博文,在上一篇的博文的基础上稍微加点东西 接下来右键解决方案添加新项目,一样建一个空的Web项目 然后在StatelessDemoWeb项目里面添加Views文件夹,Sc ...