Spring 学习(三)AOP
(1)AOP概述
- AOP:面向切面编程,扩展功能不修改源代码实现
- AOP采取横向抽取机制,取代了传统的纵向继承体系重复性代码
(2)AOP底层原理
原始方法-------》纵向继承体系

横向机制:
JDK代理机制:jdk动态代理是由Java内部的反射机制来实现的.jdk动态代理的应用前提,必须是目标类基于统一的接口。如果没有上述前提,jdk动态代理不能应用。
CGlib代理机制:Cglib的原理是对指定的目标类动态生成一个子类,并覆盖其中方法实现增强,但因为采用的是继承,所以不能对final修饰的类和final方法进行代理。

(3)AOP操作相关术语(重点切入点、增强、切面)
spring的AOP操作(基于aspectj的XML方式)
Joinpoint 连接点:类里面可以被增强的方法,这些方法称为连接点
Pointcut 切入点:在类里面可以有很多的方法被增强,但是在实际的操作中值只是增强了add()和update(),则这两个实际增强的方法称为切入点
Advice 通知/增强:实际增强的逻辑,称为增强,例如扩展日志的功能,这个日志功能称为增强
分为:
(1)前置通知:在方法之前执行
(2)后置通知:在方法之后执行
(3)异常通知:方法出现异常
(4)最终通知:在后置之后执行
(5)环绕通知:在方法之前和方法之后都执行,例如获取执行时间
Aspect 切面:把我们的Advice增强应用到具体的Pointcut切入点方法上面,这个过程称为切面
Introduction 引介 :是一种特殊的增强,它为类添加一些属性和方法。这样,即使一个业务类原本没有实现某个接口,通过AOP的引介功能,我们可以动态地为该业务类添加接口的实现逻辑,让业务类成为这个接口的实现类。
proxy 代理:一个类被AOP织入增强后,就产出了一个结果类,它是融合了原类和增强逻辑的代理类。根据不同的代理方式,代理类既可能是和原类具有相同接口的类,也可能就是原类的子类,所以我们可以采用调用原类相同的方式调用代理类。
(4)Spring的AOP操作
1、在spring里面进行AOP操作,使用 AspecJ 实现
AspectJ:面向切面的框架,它扩展了JAVA语言,是一个基于JAVA语言的AOP框架
- (1)aspectj不是spring的一部分,和spring一起使用进行AOP操作
- (2)Spring2.0之后新增加了对 aspectj 支持
2、使用AspectJ 方式实现AOP操作有两种方式
- (1)基于AspectJ 的xml配置
Book类:
package cn.itcast.aop;
public class Book {
public void add(){
System.out.println("add book......");
}
}
MyBook类:增强类对象
package cn.itcast.aop; import org.aspectj.lang.ProceedingJoinPoint; //增强类
public class MyBook {
public void befoer1(){
System.out.println("前置before1......");
}
public void after1(){
System.out.println("后置after1......");
}
//环绕通知
public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
//在方法之前执行
System.out.println("方法之前执行....."); //执行被增强的方法
proceedingJoinPoint.proceed(); //在方法之后执行
System.out.println("方法之后执行....");
}
}
AspectJ 的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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> <!--1、 配置对象 -->
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
<!-- 2、配置AOP操作 -->
<aop:config>
<!-- 2.1 配置切入点 -->
<aop:pointcut expression="execution(* cn.itcast.aop.Book.add(..))" id="pointcut1"/>
<!-- 2.2配置切面 :把增强用到方法上面 -->
<aop:aspect ref="myBook">
<!-- 配置增强类型 :method:增强的类里面,使用哪个方法作为前置增强-->
<aop:before method="befoer1" pointcut-ref="pointcut1"/>
<aop:after-returning method="after1" pointcut-ref="pointcut1"/>
<aop:around method="around1" pointcut-ref="pointcut1"/> </aop:aspect>
</aop:config>
</beans>
测试代码:
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("aopbeans.xml");
Book book=(Book)context.getBean("book");
book.add();
}
结果:
前置before1......
方法之前执行.....
add book......
方法之后执行....
后置after1......
- (2)基于aspectj 的注解方式
- 第一步:在配置文件中设置对象
<bean id="book" class="cn.sdust.aop.Book"></bean>
<bean id="myBook" class="cn.sdust.aop.MyBook"></bean>
- 第二步:开启aop操作
<!-- 开启aop操作 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- 第三步:在增强类上面使用注解完成AOP操作
Book类
package cn.sdust.aop;
public class Book {
public void add(){
System.out.println("add book.....");
}
}
MyBook类
package cn.sdust.aop; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; @Aspect
public class MyBook {
//在方法上面使用注解完成增强配置
@Before(value="execution(* cn.sdust.aop.Book.*(..))")
public void before(){
System.out.println("before......");
}
@AfterReturning(value="execution(* cn.sdust.aop.Book.*(..))")
public void after(){
System.out.println("after......");
}
@Around(value="execution(* cn.sdust.aop.Book.*(..))")
public void round(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("round--before....");
proceedingJoinPoint.proceed();
System.out.println("round--after....");
}
}
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" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- 开启aop操作 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 创建对象 -->
<bean id="book" class="cn.sdust.aop.Book"></bean>
<bean id="myBook" class="cn.sdust.aop.MyBook"></bean>
</beans>
测试代码
@Test
public void testaop(){
ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
Book book=(Book) context.getBean("book");
book.add();
}
运行结果
round--before....
before......
add book.....
round--after....
after......
AOP 操作准备:
1、jar包
- 基本jar包

- aspectj 的jar 包

2、xml中的约束有beans、context、aop

<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> </beans>
使用表达式配置切入点
1、切入点:实际增强的方法
2、常用表达式
execution (<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1)execution (* com.skd.aop.Book.add(..))//其中的(1)*表示修饰符(2)..表示如果有参数,也包括
(2)execution (* com.skd.aop.Book.*(..)) //Book/类的所有方法
(3)execution (* *.*(..))//所有类的所有方法
(4)execution (* save*(..)) //匹配save开头的所有方法
Spring 学习(三)AOP的更多相关文章
- Spring学习之AOP的实现方式
Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...
- spring学习(三) ———— spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- spring学习(二) ———— AOP之AspectJ框架的使用
前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- spring框架学习(三)——AOP( 面向切面编程)
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- spring学习三:Spring的Aop、代理
ref:https://mp.weixin.qq.com/s/J77asUvw8FcnF-6YlX6AAw AOP相关术语: Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连 ...
- Spring学习之Aop的基本概念
转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...
- Spring学习之AOP与事务
一.概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续, ...
- Spring学习三
Spring注解来注入bean 在classpath中扫描组件 组件扫描,即componetscanning 利用注解来扫描的组件有 @Component :基本注解,表示一个受Spring管理的 ...
- Spring 学习二-----AOP的原理与简单实践
一.Spring AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...
随机推荐
- WPF如何更改系统控件的默认高亮颜色 (Highlight brush)
我们在用WPF时, 经常会对系统控件的默认高亮等等颜色进行更改. 以前通常是用controlTemplate来实现. 今天发现一个更合理或者简单的方法: 用系统默认颜色的key, 比如 SystemC ...
- select元素选择时间以及jQuery对select的属性操作
<select class="input04" id="1" name="in_class1" onchange="setc ...
- 【转】 Pro Android学习笔记(三五):Menu(6):XML方式 & PopUp菜单
目录(?)[-] 利用XML创建菜单 XML的有关属性 onClick事件 Pop-up菜单 利用XML创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...
- Java探索之旅(14)——文本I/O与读写
1文件类File ❶封装文件或路径的属性.不包括创建和读写文件操作.File实例并不会实际创建文件.不论文件存在与否,可以创建任意文件名的实例.两种实例创建方式如下: ...
- /*带动画效果的hover*/
<!DOCTYPE html> /*带动画效果的hover*/ <html lang="en"> <head> <meta charset ...
- Stream接口
数据读写可以看作是事件模式(Event)的特例,不断发送的数据块好比一个个的事件.读数据是read事件,写数据是write事件,而数据块是事件附带的信息.Node 为这类情况提供了一个特殊接口Stre ...
- 1.3 xss原理分析与剖析(4)
0×01 URL编码 URL只允许用US-ASCII字符集中可打印的字符(0×20—0x7x),其中某些字符在HTTP协议里有特殊的意义,所以有些也不能使用.这里有个需要注意的,+加号代表URL编码的 ...
- 删除docker私有仓库中的镜像
1.搭建私有仓库 (1)拉取私有仓库镜像 docker pull registry(2)启动私有仓库容器 docker run ‐di ‐‐name=registry ‐p 5000:5000 reg ...
- C#识别图中二维码
1.在NuGet中添加 ZXing.Net 2.实例代码 /// <summary> /// 识别图中二维码 /// </summary> /// <param name ...
- Jquery的parent和parents(找到某一特定的祖先元素)用法
<!-- parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合. parents则是取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素).可以通过一个可选的表达式进行筛选. ...