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理解的更多相关文章

  1. spring AOP理解和相关术语

    一.AOP理解 AOP:横向抽取机制,底层使用代理方式实现. 示例: 现有LogDAO接口以及实现Log接口的Log类.类有add的方法,现在要打印add方法的开始时间和结束时间.(即增强Log的ad ...

  2. spring aop 理解

    aop简介 aop是spring 的两大特性之一,还有IOC.主要提供面向切面的编程思想,区分于面向对象编程. aop原理(动态代理+反射) 在一个方法体中,可能会存在很多其他的方法调用,我们可以把每 ...

  3. Spring aop 原始的工作原理的理解

    理解完aop的名词解释,继续学习spring aop的工作原理. 首先明确aop到底是什么东西?又如何不违单一原则并实现交叉处理呢? 如果对它的认识只停留在面向切面编程,那就脏了.从oop(Objec ...

  4. 正确理解Spring AOP中的Around advice

    Spring AOP中,有Before advice和After advice,这两个advice从字面上就可以很容易理解,但是Around advice就有点麻烦了. 乍一看好像是Before ad ...

  5. 我所理解的Spring AOP的基本概念

    Spring AOP中的概念晦涩难懂,读官方文档更是像读天书,看了非常多样例后,写一些自己理解的一些spring的概念.要理解面向切面编程,要首先理解代理模式和动态代理模式. 如果一个OA系统中的一个 ...

  6. 深入理解Spring AOP之二代理对象生成

    深入理解Spring AOP之二代理对象生成 spring代理对象 上一篇博客中讲到了Spring的一些基本概念和初步讲了实现方法,当中提到了动态代理技术,包含JDK动态代理技术和Cglib动态代理 ...

  7. 简单直白的去理解AOP,了解Spring AOP,使用 @AspectJ - 读书笔记

    AOP = Aspect Oriental Programing  面向切面编程 文章里不讲AOP术语,什么连接点.切点.切面什么的,这玩意太绕,记不住也罢.旨在以简单.直白的方式理解AOP,理解Sp ...

  8. Spring Aop的理解和简单实现

    1.AOP概念 所说的面向切面编程其实就是在处理一系列业务逻辑的时候这一系列动作看成一个动作集合.比如连接数据库来说: 加载驱动-----获取class--------获取连接对象-------访问数 ...

  9. Spring AOP深入理解之拦截器调用

    Spring AOP深入理解之拦截器调用 Spring AOP代理对象生成回想 上一篇博客中:深入理解Spring AOP之二代理对象生成介绍了Spring代理对象是怎样生成的,当中重点介绍了JDK动 ...

随机推荐

  1. 对 p 开 n 次方 (数学推论)

    #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> us ...

  2. CentOS7 笔记 (一) .NETCore

    安装系统CentOS,虚拟机好麻烦,直接在阿里云开了一个6个月免费的ECS. 熟悉Linux 基本命令 登录,exit,vi ,vim,vi保存关闭,w,ls,mkdir,df,ip addr,修改系 ...

  3. opencv(一)下载安装

    1.visual studio 工具---Nugget包管理器---管理解决方案的Nugget程序包 搜索opencv,点击安装 下载地址: 1.http://opencv.org/ 2.https: ...

  4. hashlib模块 简单了解

    import hashlib '''不可逆加密''' password = 'wwwwww7777'.encode('utf8') word = hashlib.md5(password) # md5 ...

  5. 洛谷 P2144 BZOJ 1003 [FJOI2007]轮状病毒

    题目描述 轮状病毒有很多变种.许多轮状病毒都是由一个轮状基产生.一个n轮状基由圆环上n个不同的基原子和圆心的一个核原子构成.2个原子之间的边表示这2个原子之间的信息通道,如图1. n轮状病毒的产生规律 ...

  6. 【ACM】hdu_zs1_1001_水仙花数_201307271504

    水仙花数 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)Total Submissio ...

  7. redis-不常用的命令

    在不重启redis服务器的情况下,动态的修改Redis的配置 redis> config set loglevel warning

  8. study reference

    CVPR2018 ReID论文简评 2017CVPR ICCV和NIPS在Person Reidentification方向的相关工作小结 CVPR 2018 Person Re-ID相关论文 pre ...

  9. 多个线程运行结束之后主线程再执行CountDownLatch

    多个线程运行结束之后主线程再执行CountDownLatch 学习了:http://blog.csdn.net/lvyuanj/article/details/50737123  这个要膜拜一下! h ...

  10. pl/sql developer 自带汉化选项

    pl/sql developer 自带汉化选项 版本:11.0.2 工具 -> 选项 -> 用户界面 ->外观, 第一项就是选择语言: 选择Chinese.lang,如果有的话: