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. 移动端rem.js使用方法

    下面的代码一是我根据rem的使用经验,自己写的一个rem.js,发现很好用,能适用所有移动端h5页面的自适应需求: 代码一: ``` window.onload = function(){ /*720 ...

  2. Problem 16

    Problem 16 pow(2, 15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.2的15次方等于32768,而这些数 ...

  3. Codeforces 947E Perpetual Subtraction (线性代数、矩阵对角化、DP)

    手动博客搬家: 本文发表于20181212 09:37:21, 原地址https://blog.csdn.net/suncongbo/article/details/84962727 呜啊怎么又是数学 ...

  4. 0918如何利用jmeter为数据库插入测试数据

    第一 制定测试计划,关于JMETER会通过驱动取操作数据库,因而请在底部路径填写正确. 下载该资源http://download.csdn.net/download/fnngj/3451945 第二步 ...

  5. 使用厂商MIB库查找设备OID值并实施监控的方法

    https://wenku.baidu.com/view/8f4b389e0029bd64783e2cd0.html

  6. mysql-windows修改root密码

    安装mysql完成后 直接进入方式 mysql -u root -p 设置mysql的root密码 mysql -u root -p password 8888

  7. Java-基本输入输出

    Scanner sc = new Scanner(System.in); System.out.println("Please input the path:"); String ...

  8. binlog

    binlog基本定义:二进制日志,也成为二进制日志,记录对数据发生或潜在发生更改的SQL语句,并以二进制的形式保存在磁盘中: 作用:MySQL的作用类似于Oracle的归档日志,可以用来查看数据库的变 ...

  9. 这是一个文字游戏?“这个工作你们部门牵头xx”

    近期集团一个部门在联系做一个网上应用平台系统.经过几次的会议沟通,这个原本就是解决取消个人银行卡收款的需求慢慢变成了一个在线销售加收款平台,因为其对销售的业务不熟悉,现有又有应用的软件,他们也感觉到主 ...

  10. 怎样安装g++/gdb

    该文的word文档附在文后! 链接:http://pan.baidu.com/s/1bnFcMHDpassword:z7zk