Spring---AOP
一、AOP介绍
AOP(Aspect Oriented Programing)面向切面编程:扩展功能不通过修改源代码实现
AOP采用横向抽取机制,取代传统纵向继承体系实现响应的功能(性能监控、事务、安全检查、缓存)
二、AOP的设计原理和思想
1. AOP横向抽取机制介绍
//编写一个类
public class User{
private String name;
private int age;
//方法:增加用户
public boolean addUser(String name,int age){
}
//方法:删除用户
public boolean deleteUser(String name,int age){
}
}
如今,想要在每一个方法体中都打印其所调用的方法。那么就可以是在每一个方法体中有一个输出。但是如有由成百上千种方法都要进行打印时,此方法就不适宜。
- 利用纵向抽取机制解决
//优化1:纵向抽取机制解决
public class LogUtil{
//打印功能
public void printLog(){
System.out.println("....");
System.cucurrentMills();
}
}
public class User extend LogUtil{
private String name;
private int age;
//方法:增加用户
public boolean addUser(String name,int age){
printLog();
}
//方法:删除用户
public boolean deleteUser(String name,int age){
printLog();
}
}
优点:对日志功能的修改只需要在Logutils中做修改,减少代码开发量
问题:父类中Logutils方法名称发生变化,在子类调用的方法也需要发生变化
- 利用横向抽取机制解决
底层使用: 动态代理方式实现 JDK自带的动态代理、CGLib方式实现动态代理
采用JDK自带的动态代理来介绍,必须包含接口
/接口类
public interface User{
public boolean addUser();
public boolean deleteUser();
}
//实现类
public Class UserImpl implements User {
public boolean addUser(){
伪代码
}
public boolean deleteUser(){
伪代码
}
}
实现一个代理辅助类实现invokeHandler接口,作用是增加新的功能。
动态代理创建的一个UserImpl平级的对象,真正实现的对象是和UserImpl持有共同的接口,代理对象本身是增加了新的功能,
JDK动态代理的实现中会基于代理辅助类的方法在运行时动态的产生一个新的对象,该新的对象包含原有实现类的功能实现UserImpl,还包含新增加的功能。
2.AOP工作原理
上述的示意图中已经明确表明了Spring AOP应该做什么样的工作:根据proxy提供的特定类的特定方法执行的特定时期阶段给出相应的处理建议。要完成该工作,Spring AOP应该实现:
1.确定自己对什么类的什么方法感兴趣?-----即确定 AOP的切入点(Point Cut),这个可以通过切入点(Point Cut)表达式来完成;
2. 对应的的类的方法的执行特定时期给出什么处理建议?------这个需要Spring AOP提供相应的建议 ,即我们常说的Advice。
2.AOP相关术语
连接点(JoinPoint):指的是那些被拦截到的点,在Spring这些点指的是方法,Spring中支持方法类型的连接点类可以被增强,这些方法称之为连接点
切入点(PointCut):指的是我们要对那些连接点进行拦截的定义,在类中很多的方法都可以被拦截,实际被拦截的方法称之为切入点
增强/通知(Advice):指的是拦截到连接点之后要做的事情就是通知。通知分为5中通知方式
前置通知:在真正实现方法之前执行
后置通知:在方法之后执行
异常通知:在方法执行出现异常时才执行
最终通知:在后置通知之后才执行
环绕通知:在方法之前和之后执行
切面(Aspect):是切入点和通知的结合,把通知应用到切入点的过程
引介(Introduction):引介是一种特殊的通知在不修改代码的前提下,引介可以在运行期为类动态的添加一些方法或属性
目标对象(Target):代理的目标对象,要增强的类
代理(Proxy):一个被AOP织入增强后,就铲射了一个结果代理类
织入(Weaving):是把通知应用到目标对象的过程,把advice应用到target的过程
三、 AOP基于Aspectj配置的实现
在spring中AOP的使用是通过Aspectj第三方框架实现的,Aspectj是java语言实现的一个专门的面向切面编程的框架
Aspectj不是spring框架的一部分,和Spring一起实现AOP的操作
1.引入AOP相关依赖
spring的基本依赖之外,在添加AOP的依赖
<!--spring AOP相关jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
2.引入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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
3.业务实现类
public class Student {
public void addStudent(){
System.out.println("Student.addStudent");
}
}
4.增强类:添加日志功能
public class DIYLog {
public void writeLog(){
System.out.println("DIYLog.writeLog");
}
}
5.通过表达式来配置切入点
execution函数介绍
在实现通知的过程中,通过execution函数,可以定义切入点的方法切入
格式:execution(<访问修饰符>?<返回类型><方法名>(<参数>) <异常> )
回顾:方法格式:访问限定符 返回类型 方法名(参数) 异常
(1)execution(* com.tulun.bean.Book.show(…)) 表类里面的某一个方法
(2)execution(* com.tulun.bean.Book.(…)) 表类某个包里类所有方法
(3)execution( .(…)) 表示所有
例:
-匹配所有类public方法 execution(public .(…))
-匹配指定包下所有类方法 execution(* com.tulun.bean.(…)) (不包含子包)
-execution( com.tulun.bean…(…)) (包含包、子包下所有类)
-匹配指定类所有方法 execution( com.tulun.bean.Book.(…))
-匹配实现特定接口所有类方法 execution( com.tulun.bean.Book+.(…))
-匹配所有com开头的方法 execution( com*(…))
6.配置AOP操作
<!--配置对象-->
<bean id="student" class="com.tulun.bean.Student"/>
<bean id="log" class="com.tulun.bean.DIYLog"/>
<!--配置AOP操作-->
<aop:config>
<!--配置切入点:使用execution表达式-->
<aop:pointcut id="pointcut1" expression="execution(* com.tulun.bean.Student.addStudent())"/>
<!--配置切面:把通知应用到方法的过程-->
<aop:aspect ref="log">
<!--配置增强类型 method属性:指定曾倩类中的那个方法-->
<aop:before method="writeLog" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
7.使用
//获取IOC容器,通过读取classpath路径下的spring的配置文件
String path = "spring-aop.xml";
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext(path);
//在IOC容器获取需要的对象实例
Student student = (Student) applicationContext.getBean("student");
student.addStudent();
8.环绕
/**
* 环绕通知,必须有ProceedingJoinPoint类型的参数,该参数调用proceed表示执行真正的实现
*/
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("方法之前执行");
//执行真正的实现方法
joinPoint.proceed();
System.out.println("方法之后执行");
}
9.各种通知类型配置如下:
<!--前置通知:aop:before-->
<aop:before method="writeLog1" pointcut-ref="pointcut1"/>
<!--后置通知:aop:after-->
<aop:after method="writeLog1" pointcut-ref="pointcut1"/>
<!--最终通知:aop:after-returning-->
<aop:after-returning method="writeLog2" pointcut-ref="pointcut1"/>
<!--异常通知:aop:after-throwing-->
<aop:after-throwing method="around" pointcut-ref="pointcut1"/>
<!--环绕通知:aop:around-->
<aop:around method="around" pointcut-ref="pointcut1"/>
四、AOP基于注解形式实现
1.开启AOP得注解操作
<!--打开AOP注解-->
<aop:aspectj-autoproxy/>
2.增强类添加上注解
@Aspect
public class DIYLog {
@Before(value = "execution(* com.tulun.bean.Student.addStudent(..))")
@After(value = "execution(* com.tulun.bean.Student.addStudent(..))") //后置通知
@AfterReturning //最终通知
@AfterThrowing //异常通知
public void writeLog1(){
System.out.println("DIYLog.writeLog1");
}
/**
* 环绕通知,必须有ProceedingJoinPoint类型的参数,该参数调用proceed表示执行真正的实现
*/
@Around(value = "execution(* com.tulun.bean.Student.addStudent(..))") //环绕通知
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("方法之前执行");
//执行真正的实现方法
joinPoint.proceed();
System.out.println("方法之后执行");
}
}
注意:
AOP相关操作的注解一定是在增强类和方法上
在增强类上添加注解@Aspect
在增强类中响应方法上根据不同的增强类型添加上不同注解
@Around(value = “execution(* com.tulun.bean.Student.addStudent(…))”) //环绕通知
@Before(value = “execution(* com.tulun.bean.Student.addStudent(…))”)
@After(value = “execution(* com.tulun.bean.Student.addStudent(…))”) //后置通知
@AfterReturning //最终通知
@AfterThrowing //异常通知