Spring学习(23)--- AOP之Introductions应用
- 简介允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象
- 由<aop:aspect>中的<aop:declare-parents>元素声明该元素用于声明所匹配的类型拥有一个新的parents(因此得名)
配置:
<aop:aspect id="usageTrackerAspect" ref="usageTracking">
<aop:declare-parents types-matching="com.xyz.myapp.service.*+"
implement-interface="com.xyz.myapp.service.tracking.UsageTracked"
default-impl="com.xyz.myapp.service.tracking.DefaultUsageTracked"/>
</aop:aspect>
public void recordUsage(){
usageTracked.incrementUseCount();
}
UsageTracked usageTracked = (UsageTracked) context.getBean("myservice");
例子:
新建接口Fit及实现类FitImpl
package com.aop.schema;
public interface Fit {
void filter();
}
package com.aop.schema;
public class FitImpl implements Fit {
@Override
public void filter() {
System.out.println("FitImpl.filter");
}
}
切面类(同上节,不做修改了):
package com.aop.schema.advice; import org.aspectj.lang.ProceedingJoinPoint; /**
*
* 切面类
*
*/
public class MyAspect { public void before(){
System.out.println("MyAspect.before");
} public void afterreturning(){
System.out.println("MyAspect.afterreturning");
} public void afterthrowing(){
System.out.println("MyAspect.afterthrowing");
} public void after(){
System.out.println("MyAspect.after");
} public void around(ProceedingJoinPoint pjp) {
try {
System.out.println("MyAspect.around_1");
Object obj=pjp.proceed();
System.out.println("MyAspect.around_2");
} catch (Throwable e) {
e.printStackTrace();
}
} public void around_init(ProceedingJoinPoint pjp,String name,int age) {
System.out.println(name+" "+age);
try {
System.out.println("MyAspect.around_1");
Object obj=pjp.proceed();
System.out.println("MyAspect.around_2");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
XML配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <bean id="myAspect" class="com.aop.schema.advice.MyAspect"></bean>
<aop:config>
<aop:aspect id="myAspectAOP" ref="myAspect">
<!-- com.aop.schema.advice.*+ 指的是advice包下的所有类 -->
<aop:declare-parents types-matching="com.aop.schema.advice.*+"
implement-interface="com.aop.schema.Fit"
default-impl="com.aop.schema.FitImpl"/>
</aop:aspect>
</aop:config> </beans>
单元测试:
package com.aop.schema; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.aop.schema.advice.ApsectBiz; public class UnitTest { @Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-aop.xml");
Fit fit = (Fit)context.getBean("myAspect");
fit.filter();
}
}
测试结果:
七月 11, 2015 1:15:13 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@31b3c607: startup date [Sat Jul 11 13:15:13 CST 2015]; root of context hierarchy
七月 11, 2015 1:15:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-aop.xml]
FitImpl.filter
PS:说明为指定的类型强制指定了一个父类。
Spring学习(23)--- AOP之Introductions应用的更多相关文章
- Spring学习之AOP的实现方式
Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- spring学习(二) ———— AOP之AspectJ框架的使用
前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...
- Spring 学习二-----AOP的原理与简单实践
一.Spring AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...
- Spring学习之AOP
Spring-AOP(Aspect-orented programming) 在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutt ...
- Spring学习之Aop的各种增强方法
AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...
- Spring学习之Aop的基本概念
转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...
- Spring学习之AOP与事务
一.概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续, ...
- spring学习笔记-AOP
1.aop:aspect oriented programming 面向切面编程 2.aop在spring中的作用: 提供声明式服务(声明式事务) 允许用户实现自定义切面 3.aop:在不改变原有 ...
- Spring 学习之AOP
1. 走进面前切面编程 编程范式: 面向过程编程,c语言: 面向对象编程:c++,java,c#; 函数式编程: 事件驱动编程: 面向切面编程: AOP是一种编程范式,不是编程语言:解决特定问题,不能 ...
随机推荐
- css代码实现
纯 CSS 实现下面我们探讨下,使用纯 CSS 的方式能否实现. hover 伪类实现 使用 hover 伪类,在鼠标悬停在按钮上面时,控制动画样式的暂停. 关键代码如下: <div class ...
- bzoj3112 [Zjoi2013]防守战线
正解:线性规划. 直接套单纯形的板子,因为所约束条件都是>=号,且目标函数为最小值,所以考虑对偶转换,转置一下原矩阵就好了. //It is made by wfj_2048~ #include ...
- UBIFS文件系统介绍
1. 引言 UBIFS,Unsorted Block Image File System,无排序区块图像文件系统.它是用于固态硬盘存储设备上,并与LogFS相互竞争,作为JFFS2的后继文件系统之一 ...
- 蓝桥杯-搭积木-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...
- sublime text 3文件标题无法显示中文的解决办法
前言 你是否遇也到了sublime文件标题无法显示中文的问题呢?如下列图片所示: 解决办法 1.点击菜单栏Preferences - > Settings. 打开下图: 2.在右边User内加入 ...
- Android学习资料整理
1.官方网站 http://developer.android.com/index.html http://android-developers.blogspot.com/ 2.Android Des ...
- dubbo 入门
1 介绍 1.1 背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 1.2 说明 DUBB ...
- VMTools安装
先启动CentOS并成功登陆如下图,发现底部提示,准备安装 2.选择虚拟机菜单栏--安装VMware tools 3.光驱目录中拷贝VMwareTools-10.0.5-3228253.tar.gz到 ...
- CATransition 转场动画解析
http://blog.csdn.net/mad2man/article/details/17260901
- ArrayList源码解析(三)
1.isEmpty() 如果此列表中没有元素,则返回 true /** * Returns <tt>true</tt> if this list contains no el ...