Spring AOP理解
Spring的核心思想的IOC和AOP。最近学习AOP,对切面的学习有了进一步的认识。
Spring用代理类包裹切面,把他们织入到Spring管理的bean中。也就是说代理类伪装成目标类,它会截取对目标类中方法的调用,让调用者对目标类的调用都先变成调用伪装类,伪装类中就先执行了切面,再把调用转发给真正的目标bean。这样可以实现对业务代码的最小化侵入。使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性。Spring中日志记录,性能统计,安全控制,事务处理等都是通过AOP来管理的。

伪装类的实现有两种方式:1.实现和目标类相同的接口。这种兄弟模式下,spring会使用JDK的java.lang.reflect.Proxy类,它允许Spring动态生成一个新类来实现必要的接口,织入通知,并且把对这些接口的任何调用都转发到目标类。
2.生成子类调用,用子类来做为伪装类。这种父子模式下,spring使用CGLIB库生成目标类的一个子类,在创建这个子类的时候,spring织入通知,并且把对这个子类的调用委托到目标类。
相比之下,还是兄弟模式好些,能更好的实现松耦合,尤其在今天都高喊着面向接口编程的情况下,父子模式只是在没有实现接口的时候,也能织入通知,应当做一种例外。
下面看一个AOP的小应用。目的是为了在addStudent方法执行前后通过AOP来进行相关操作。
在上下文中配置aspectJ。
<aop:aspectj-autoproxy/>
//定义切面,切点。也可以通过注解来实现切面的和切点标识。
<aop:config>
<aop:aspect id="StudentServiceAspect" ref="studentServiceAspect">
<aop:pointcut id="businessService" expression="execution(* com.lufax.test.commen.studentService.*.*(..))"/>
<aop:before method="doBefore" pointcut-ref="businessService"/>
<aop:after method="doAfter" pointcut-ref="businessService"/>
<aop:around method="doAround" pointcut-ref="businessService"/>
</aop:aspect>
</aop:config>
下面定义studentService的接口。
public interface StudentService {
public void addStudent(String name);
}
studentService的实现类如下。
public class StudentServiceImp implements StudentService {
public void addStudent(String name){
System.out.println("----正在添加" + name+"----");
}
}
以上接口和实现都是正常的业务逻辑。下面写切面。
public class StudentServiceAspect {
//在业务代码执行前运行
public void doBefore(){
System.out.println("类名:");
System.out.println("doBefore:开始添加学生");
}
//业务代码执行后运行
public void doAfter(JoinPoint jp){
System.out.println("doAfter:添加完毕");
//可以通过Joinpoint类来实现日志的打印。记录切点下类和方法名、参数,以便后续快速定位问题所在。
System.out.println("[类名+"+jp.getTarget().getClass().getName()+"],"+
"[方法名:"+jp.getSignature().getName()+"],"+
"[参数:"+jp.getArgs()[0]+"]");
}
//环绕通知,retVal是返回值。这里为null
public Object doAround(ProceedingJoinPoint pjp)throws Throwable{
System.out.println("doAround:加入前");
Object retVal= pjp.proceed();
System.out.println(retVal);
System.out.println("doAround:加入后");
return retVal;
}
}
通过注解实现。
@Aspect
public class StudentServiceAspect { @Before(value="execution(* com.lufax.test.commen.studentService.*.addStudent(..)) ")
public void doBefore(){
System.out.println("类名:");
System.out.println("doBefore:开始添加学生");
} @After(value="execution(* com.lufax.test.commen.studentService.*.addStudent(..))")
public void doAfter(JoinPoint jp){
System.out.println("doAfter:添加完毕");
System.out.println("[类名+"+jp.getTarget().getClass().getName()+"],"+
"[方法名:"+jp.getSignature().getName()+"],"+
"[参数:"+jp.getArgs()[0]+"]");
} @Around(value="execution(* com.lufax.test.commen.studentService.*.addStudent(..))")
public Object doAround(ProceedingJoinPoint pjp)throws Throwable{
System.out.println("doAround:加入前");
Object retVal= pjp.proceed();
System.out.println(retVal);
System.out.println("doAround:加入后");
return retVal;
}
}
最后写测试类。
@Test
public void test_AopAspectJ() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","dataSource.xml");
StudentService studentService = (StudentService) applicationContext.getBean("studentService");
studentService.addStudent("张三");
}
运行结果如下:
类名:
doBefore:开始添加学生
doAround:加入前
----正在添加张三----
doAfter:添加完毕
[类名+com.lufax.test.commen.studentService.StudentServiceImp],[方法名:addStudent],[参数:张三]
null
doAround:加入后
Spring AOP理解的更多相关文章
- spring AOP理解和相关术语
一.AOP理解 AOP:横向抽取机制,底层使用代理方式实现. 示例: 现有LogDAO接口以及实现Log接口的Log类.类有add的方法,现在要打印add方法的开始时间和结束时间.(即增强Log的ad ...
- spring aop 理解
aop简介 aop是spring 的两大特性之一,还有IOC.主要提供面向切面的编程思想,区分于面向对象编程. aop原理(动态代理+反射) 在一个方法体中,可能会存在很多其他的方法调用,我们可以把每 ...
- Spring aop 原始的工作原理的理解
理解完aop的名词解释,继续学习spring aop的工作原理. 首先明确aop到底是什么东西?又如何不违单一原则并实现交叉处理呢? 如果对它的认识只停留在面向切面编程,那就脏了.从oop(Objec ...
- 正确理解Spring AOP中的Around advice
Spring AOP中,有Before advice和After advice,这两个advice从字面上就可以很容易理解,但是Around advice就有点麻烦了. 乍一看好像是Before ad ...
- 我所理解的Spring AOP的基本概念
Spring AOP中的概念晦涩难懂,读官方文档更是像读天书,看了非常多样例后,写一些自己理解的一些spring的概念.要理解面向切面编程,要首先理解代理模式和动态代理模式. 如果一个OA系统中的一个 ...
- 深入理解Spring AOP之二代理对象生成
深入理解Spring AOP之二代理对象生成 spring代理对象 上一篇博客中讲到了Spring的一些基本概念和初步讲了实现方法,当中提到了动态代理技术,包含JDK动态代理技术和Cglib动态代理 ...
- 简单直白的去理解AOP,了解Spring AOP,使用 @AspectJ - 读书笔记
AOP = Aspect Oriental Programing 面向切面编程 文章里不讲AOP术语,什么连接点.切点.切面什么的,这玩意太绕,记不住也罢.旨在以简单.直白的方式理解AOP,理解Sp ...
- Spring Aop的理解和简单实现
1.AOP概念 所说的面向切面编程其实就是在处理一系列业务逻辑的时候这一系列动作看成一个动作集合.比如连接数据库来说: 加载驱动-----获取class--------获取连接对象-------访问数 ...
- Spring AOP深入理解之拦截器调用
Spring AOP深入理解之拦截器调用 Spring AOP代理对象生成回想 上一篇博客中:深入理解Spring AOP之二代理对象生成介绍了Spring代理对象是怎样生成的,当中重点介绍了JDK动 ...
随机推荐
- Html 页面刷新后出现闪动
Html 页面刷新后,或跳转后,出现闪动,抖动问题 1.查看有没有用到新字体,新字体链接位置是否存在 如: @font-face { font-family: "AvantGarde-Dem ...
- 移动端出现弹出层body滚动
$("#box").on("click",function(e){ e.stopPropagation(); e.preventDefault(); $(&qu ...
- jQuery升级踩坑之路
1.使用了被废弃的jQuery.browser属性 jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support . 在更 ...
- PAT 1079. Total Sales of Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- android 权限清单
常用权限: 读写存储卡装载和卸载文件系统 android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_EXTERNAL_STOR ...
- mysql deadlock、Lock wait timeout解决和分析
项目上线 线上遇到大量的deadlock 和wait timeout 但是看程序没什么问题 问dba也不能给出很好的解决方案!最终自己去了解mysql锁 以及看mysq锁日志 如果了解mysql锁的机 ...
- poj 1466 最大独立集
#include<stdio.h> #include<string.h>//这个分开后男的站在一边女的站在一边,不肯能有les或者gay.最大独立集=n-最大匹配数 #defi ...
- mysqldump中使用flush tables with read lock的风险分析
http://blog.csdn.net/wireless_tech/article/details/7332906 我们使用mysqldump --single-transaction -- ...
- C++关键知识
<精通MFC>第一章节整理复习 //c++编程技术要点 /* //1.虚函数及多态的实现 //演示多态技术 #include <iostream> using namespac ...
- 64bit Centos6.4搭建hadoop-2.5.1
64bit Centos6.4搭建hadoop-2.5.1 1.分布式环境搭建 採用4台安装Linux环境的机器来构建一个小规模的分布式集群. 当中有一台机器是Master节点,即名称节点,另外三台是 ...