spring笔记(二)——aop
面对知识我们喜欢使用what(是什么),how(怎么做),why(为什么)理解这个知识含义。接下来的博客,就是使用这三个我们高中学习知识的方法解答问题。
1、什么是aop?
1.1 aop概念(what)
AOP (Aspect Orient Programming),直译过来就是 面向切面编程。AOP 是一种编程思想,是面向对象编程(OOP)的一种补充。面向对象编程将程序抽象成各个层次的对象,而面向切面编程是将程序抽象成各个切面。
1.2 为什么要使用aop(why)
我们在日常写代码的时候,在一些模块中会出现大量重复的代码片段,我们没学aop的时候使用复制、粘贴的方式进行的,但是,我们学面向过程编程的时候,我们可以把相同的代码抽象成一个方法,我们可以在调用这些方法,我们会发现,如果要修改一个功能的时候需要大改代码,这样是很不方便的,那么有没有一种方法可以帮助我们进行修改代码呢?
当然有了那就是aop。
1.3 我们怎么使用aop(how)
-
我们需要了解aop中的术语
通知(Advice): AOP 框架中的增强处理。通知描述了切面何时执行以及如何执行增强处理。
连接点(join point): 连接点表示应用执行过程中能够插入切面的一个点,这个点可以是方法的调用、异常的抛出。在 Spring AOP 中,连接点总是方法的调用。
切点(PointCut): 可以插入增强处理的连接点。
切面(Aspect): 切面是通知和切点的结合。
引入(Introduction):引入允许我们向现有的类添加新的方法或者属性。
织入(Weaving): 将增强处理添加到目标对象中,并创建一个被增强的对象,这个过程就是织入。
这么多,是不是很难理解。接下来我画一张图面和具体的解释,大家就清楚了。
2. 怎么实现的aop
Spring中AOP代理由Spring的IOC容器负责生成、管理,其依赖关系也由IOC容器负责管理。因此,AOP代理可以直接使用容器中的其它bean实例作为目标,这种关系可由IOC容器的依赖注入提供。Spring创建代理的规则为:(动态代理)我们就称为生成假儿子
1、默认使用Java动态代理来创建AOP代理,这样就可以为任何接口实例创建代理了
2、当需要代理的类不是代理接口的时候,Spring会切换为使用CGLIB代理,也可强制使用CGLIB
2、代码编写
这个是从网上找的一个案例进行解释一下
- 第一步就是导入所需要的jar。
- 定义我们要实现的切面类(CheckAdvice)和需要被加强的类(IeacherImpl)。
package com.Advices;
public class CheckAdvice {
public void doPrint(){
System.out.println("这个是切面类的方式");
}
}
package com.interfaces;
public interface ITeacher {
public void save();
public void update();
public void delete();
public void select();
}
package com.interfaces.impl;
import com.interfaces.ITeacher;
public class IeacherImpl implements ITeacher {
@Override
public void save() {
System.out.println("这个是.. save");
}
@Override
public void update() {
System.out.println("这个是.. update");
}
@Override
public void delete() {
System.out.println("这个是.. delete");
}
@Override
public void select() {
System.out.println("这个是.. select");
}
}
- 写出applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="TeacherDao" class="com.interfaces.impl.IeacherImpl"></bean>
<bean id="CheckAdvice" class="com.Advices.CheckAdvice"></bean>
<aop:config>
<!--定义切入点-->
<aop:pointcut id="savePoint" expression="execution(* com.interfaces.ITeacher.save(..))"/>
<!-- 定义切面-->
<aop:aspect ref="CheckAdvice">
<aop:before method="doPrint" pointcut-ref="savePoint"></aop:before>
</aop:aspect>
</aop:config>
</beans>
这里解释一下:
我们bean化了两个类,分别是起了名称:TeacherDao和CheckAdvice。
定义切面<aop>标签,先定义切入点,我们是要给save()方法加强,所以我们要使用表达式,如果加强这个包中所有类的save()方法,那么我们只需要把要全部概括的地方换成*即可。(详解1具体讲解)
定义切面,ref是引入切面类,aop:before的标签,查找一个叫doPrint的方法,加强在以id为savePoint的切入点。(详解2讲述别的通知方式)
详解:
详解1:
任意公共方法的执行:
execution(public * (…))
任何一个以“set”开始的方法的执行:
execution( set*(…))
UserService接口的任意方法的执行:
execution(* com.cheng.aop.service.UserService.(…))
定义在service包里的任意类和任意方法的执行:
execution( com.cheng.aop.service..(…))
定义在service包和所有子包里的任意类的任意方法的执行(比上面多了个点):
execution(* com.cheng.aop.service….(…))
定义在pointcutexp包和所有子包里的UserService的的任意方法的执行:
execution(* com.cheng.aop.service…UserService.*(…))
详解2:
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="pc001"/>
<!-- 环绕通知 -->
<aop:around method="around" pointcut-ref="pc002" />
<!-- 后置通知 -->
<aop:after-returning method="afterReturning" pointcut-ref="pc003" returning="result"/>
<!-- 最终通知 都织入pc003(deleteUser)中,看看后置和最终谁最后执行 -->
<aop:after method="after" pointcut-ref="pc003"/>
<!-- 异常通知 -->
<aop:after-throwing method="afterThrow" pointcut-ref="pc004" throwing="e"/>