Spring中AOP开发步骤
AOP:不是由Spring定义.AOP联盟的组织定义.Spring中的通知:(增强代码)前置通知 org.springframework.aop.MethodBeforeAdvice* 在目标方法执行前实施增强后置通知 org.springframework.aop.AfterReturningAdvice* 在目标方法执行后实施增强环绕通知 org.aopalliance.intercept.MethodInterceptor* 在目标方法执行前后实施增强异常抛出通知 org.springframework.aop.ThrowsAdvice* 在方法抛出异常后实施增强
第一步:导入相应jar包.
* spring-aop-3.2.0.RELEASE.jar
* com.springsource.org.aopalliance-1.0.0.jar
第二步:编写被代理对象:
* CustomerDao接口
* CustoemrDaoImpl实现类
第三步:写一个类继承相关Advice的接口:
public class MyBeforeAdvice implements MethodBeforeAdvice{/*** method:执行的方法* args:参数* target:目标对象*/public void before(Method method, Object[] args, Object target)throws Throwable {System.out.println("前置增强...");}
}
第四步:生成代理:(配置生成代理:)
spring是支持配置文件帮我们生成代理的。<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 目标对象 --><bean id="customerDao" class="cn.itcast.aop.dao.CustomerDaoImpl"/><!-- 定义增强对象 -->
<bean id="beforeAdvice" class="cn.itcast.aop.advice.MyBeforeAdvice"/>
<!-- 使用spring框架生成代理对象 -->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理的目标对象 -->
<property name="target" ref="customerDao"></property>
<!-- 设置实现的接口告诉代理实现的接口跟CustomerDaoImpl实现类实现的接口一样 -->
<property name="proxyInterfaces" value="cn.itcast.aop.dao.CustomerDao"></property>
<!-- 增强是通知name="interceptorNames"拦截所有的方法 value="beforeAdvice" 增强的通知告诉代理对象应该使用前置增强-->
<property name="interceptorNames" value="beforeAdvice"></property>
</bean></beans>
第五步:编写测试类
利用spring集成junit将我们需要的目标类注入到测试类中
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest {@Autowired// @Qualifier("customerDao")//注入真实对象@Qualifier("customerDaoProxy")//注入代理对象private CustomerDao customerDao;@Testpublic void test(){customerDao.add();customerDao.delete();customerDao.find();customerDao.update();}
}
Spring中AOP开发步骤相关推荐
- spring中AOP(面向切面编程)
spring中AOP(面向切面编程) 面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是spring框架中的一个重要内容 ...
- spring中aop事务
文章目录 事务 为什要用到Spring中AOP事务 事物的特性 ACID 事务并发问题 事务的隔离级别 spring事务管理 事务操作 事务操作对象 spring管理事务的属性介绍 spring管理事 ...
- Spring中AOP源码剖析
Spring中AOP源码剖析 关键词 aop的增强发生在后置处理器中(没有循环依赖) 最终增强是通过 递归调用 ,层层增强 一.环境准备 1.1 bean和接口 public class AopBea ...
- Spring系统学习--20180613:Spring的AOP开发说明
Spring的AOP开发说明: 都是基于AspectJ的开发:其中,AOP的基于AspectJ的开发有两种方式,分别是基于AspectJ的XML方式 和 基于 AspectJ的注解方式. 这和Spri ...
- Spring中AOP实现
1.什么是SpringAOP 什么是aop:Aspect Oriented Programming的缩写,面向切面编程,通过预编译和动态代理实现程序功能的 统一维护的一种技术 主要功能:日志记录,性能 ...
- 动态代理以及对应Spring中AOP源码分析
AOP(面向切面编程)在Spring中是被广泛应用的(例如日志,事务,权限等),而它的基本原理便是动态代理. 我们知道动态代理有两种:基于JDK的动态代理以及基于CGlib动态代理.以下是两种动态代理 ...
- Spring中AOP相关的API及源码解析,原来AOP是这样子的
前言 之所以写这么一篇文章主要是因为下篇文章将结束Spring启动整个流程的分析,从解析配置到创建对象再到属性注入最后再将创建好的对象初始化成为一个真正意义上的Bean.因为下篇文章会涉及到AOP,所 ...
- spring中AOP动态代理的两种方式
AOP动态代理的两种方式 Spring AOP动态代理的方式(spring的AOP默认是JDK Proxy) 浅谈这两种动态代理 JDK的动态代理,需要有实现接口 动态代理--JDK Proxy ⚫ ...
- Spring中AOP的使用
问题:什么是AOP? 答:AOP基本概念:Aspect-Oriented Programming,面向方面编程的简称,Aspect是一种新的模块化机制,用来描述分散在对象.类或方法中的横切关注点(cr ...
最新文章
- 打造计数君!谷歌提出RepNet:可自动计数视频重复片段 | CVPR 2020
- mysql getinstance_php设计模式之单例模式使用示例
- lazada开店入驻最新流程,开店只需三步
- FckEditor的安装与设置
- C语言 防止头文件被多次引用
- 2017-07-22 模拟赛
- php打出等边三角形,CSS 如何进行单一div的正多边形变换
- ggplot2作图详解:映射(mapping)
- Harmony OS — Picker滑动选择器
- 未开启3389实现远程桌面
- CSS之BFC(Block Formatting Context)
- app标签栏android,Android Navigation TabBar控件实现多彩标签栏
- C程序设计(第五版)谭浩强第七章课后答案
- 负重下肢外骨骼的运动意图预测和运动状态辨识
- java之家_java之家
- c语言求100以内偶数和while,c语言 求1到100以内的偶数之和
- C语言中常量后缀,u或U,l或L,f或F问题
- 洛谷.P3374 树状数组
- L0,L1,L2正则化浅析
- 阿里云OSS远程连接
热门文章
- java 给对象创建实例_Java中创建(实例化)对象的五种方式
- weex监听android返回键,weex使用手册
- android微信h5转原生支付,微信原生支付流程以及踩坑
- linux下搜狗安装目录,Linux安装搜狗输入法
- 四二拍用音符怎么表示_每个音符都是赞美歌拍子分析 0基础识简谱每日必看
- 【Maven】Eclipse中的Maven项目安装与配置
- 【测试】接口测试介绍
- 数据分析--使用Web API(应用编程接口)以自动请求GitHub网站的特定信息为例爬取数据,附完整的Python代码详解
- 洛谷 P1219 八皇后题解
- RT-Thread中如何预编译一个.c文件