什么是AOP?
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
我们在来看一下AOP和OOP的区别:AOP是OOP的补充,当我们需要为多个对象引入一个公共行为,比如日志,操作记录等,就需要在每个对象中引用公共行为,这样程序就产生了大量的重复代码,使用AOP可以完美解决这个问题。
接下来介绍一下提到AOP就必须要了解的知识点:
切面:拦截器类,其中会定义切点以及通知
切点:具体拦截的某个业务点。
通知:切面当中的方法,声明通知方法在目标业务层的执行位置,通知类型如下:
前置通知:@Before 在目标业务方法执行之前执行
后置通知:@After 在目标业务方法执行之后执行
返回通知:@AfterReturning 在目标业务方法返回结果之后执行
异常通知:@AfterThrowing 在目标业务方法抛出异常之后
环绕通知:@Around 功能强大,可代替以上四种通知,还可以控制目标业务方法是否执行以及何时执行
AOP的相关术语
- Joinpoint(连接点):
所谓连接点是指那些被拦截到的点。在 spring 中,这些点指的是方法,因为 spring 只支持方法类型的连接点。
- Pointcut(切入点):
所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义。
- Advice(通知/增强):
所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知。通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。
- Introduction(引介):
引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field。
- Target(目标对象):
代理的目标对象。
- Weaving(织入):
是指把增强应用到目标对象来创建新的代理对象的过程。spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装载期织入。
- Proxy(代理):
一个类被 AOP 织入增强后,就产生一个结果代理类。
- Aspect(切面):
是切入点和通知(引介)的结合。
AOP配置xml入门
首先导入IOC和AOP所需要的jar包
编写核心类接口
public interface BookService {
int save(int n);
int del();
int update();
void find();
}
实现核心类的抽象方法
public class BookServiceImpl implements BookService {
@Override
public int save(int n) {
System.out.println("添加");
return 1;
}
@Override
public int del() {
System.out.println("删除");
return 1;
}
@Override
public int update() {
System.out.println("修改");
return 1;
}
@Override
public void find() {
System.out.println("查询");
}
}
编写增强类
public class Loger {
public void check(){
System.out.println("前置通知/增强:执行日志的打印");
}
public void logPrint(){
System.out.println("后置通知/增强:执行日志的打印");
}
public void exception(){
System.out.println("异常通知/增强:做出异常处理");
}
public void distory(){
System.out.println("最终通知/增强:资源释放");
}
}
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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1.把所有类的对象交给IOC容器进行管理-->
<bean id="loger" class="com.zeng.advice.Loger"/>
<bean id="bookService" class="com.zeng.service.impl.BookServiceImpl"/>
<!--2.AOP的配置:让增强类 的 哪个方法 动态进行何种增强 核心类 的 哪个方法-->
<aop:config>
<!--配置 增强类的哪个方法 对 核心类的哪个方法 进行 何种增强-->
<aop:aspect id="log" ref="loger">
<aop:before method="check" pointcut="execution(* *..BookServiceImpl.*(..))"/>
<aop:after-returning method="logPrint" pointcut="execution(* *..BookServiceImpl.*(..))"/>
<aop:after-throwing method="exception" pointcut="execution(* *..BookServiceImpl.*(..))"/>
<aop:after method="distory" pointcut="execution(* *..BookServiceImpl.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
编写测试类
public class Test01 {
@Test
public void testo1(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
BookService bookService=context.getBean(BookService.class);
bookService.save(5);
}
}