AOP(面向切面编程)
- 概念 :
- 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术
- AOP是Spring框架中的一个重要内容,是函数式编程的一种衍生泛型。利用AOP可以对业务逻辑的各个部分进行隔离,使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高开发的效率
- 作用 :
- 在程序运行期间,在不修改源码的情况下对方法进行功能增强
- 优势 :
- 减少重复代码,提高开发效率,并且便于维护
- 底层实现
- 通过Spring提供的动态代理技术实现的,在运行期间,Spring通过动态代理技术动态的生成代理对象,代理对象方法执行时进行增强功能的介入,在去调用目标对象的方法,从而完成功能的增强
- 动态代理技术 :
- JDK代理 : 基于接口的动态代理技术
- cglib代理 : 基于父类的动态代理技术
JDK代理
public class ProxyTest {
public static void main(String[] args) {
//创建要代理的目标对象
final Target target = new Target();
//获得增强对象
Advice advice = new Advice();
//返回值,就是动态生成的代理对象
TargetInterface proxyInstance = (TargetInterface) Proxy.newProxyInstance(
target.getClass().getClassLoader(), //目标对象类加载器
target.getClass().getInterfaces(), //目标对象相同的接口字节码对象的值
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//前置增强
advice.befor();
method.invoke(target, args);//执行目标方法
//后置增强
advice.later();
return null;
}
}
);
//调用代理对象的方法
proxyInstance.save();
}
}
cglib的动态代理
public class ProxyTest {
public static void main(String[] args) {
//目标对象
final Target target = new Target();
//增强对象
final Advice advice = new Advice();
//返回值就是动态生成的代理对象,基于cglib
//1.创建增强器
Enhancer enhancer = new Enhancer();
//2.设置父类(目标)
enhancer.setSuperclass(Target.class);
//3.设置回调
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
advice.befor(); //执行前置
Object invoke = method.invoke(target, args);//执行目标
advice.later(); //执行后置
return invoke;
}
});
//4.创建代理对象
Target proxy = (Target) enhancer.create();
proxy.save();
}
}
AOP相关概念
- Target( 目标对象 ) : 代理的目标对象
- Proxy(代理) : 一个类被AOP增强后,就产生一个结果代理类
- Jointpoint(连接点) : 所谓连接点是指那些被拦截到的点,在Spring中,这些点指的是方法,因为spring只支持方法类型的连接点
- Pointcut(切入点) : 所谓切入点是指我们要对那些 Joinpoint 进行拦截的定义
- Advice ( 通知/增强 ) : 所谓通知是指拦截的Jointpoint 之后要做的事情就是通知
- Aspect(切面) : 是切入点和通知的结合
- Weaving(织入) : 是指把增强应用到目标对象来创建新的代理对象的过程,spring采用动态代理织入,而AspectJ 采用编译期织入和类加载期织入
- AOP开发明确的事项
- 编写核心业务代码(目标类的目标方法)
- 编写切面类,切面类中有通知(增强功能方法)
- 在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合
- AOP技术实现的内容
- Spring框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代理逻辑运行
- AOP底层使用那种代理方式
- 在spring中框架会根据目标类是否实现了接口来决定采用那种代理的方式
基于XML的AOP开发
-
快速入门
- 导入AOP相关坐标 ( spring-context, aspectjweaver)
- 创建目标接口和目标类
- 创捷切面类(内部有增强方法)
- 将目标类和切面类的对象创建权交给Spring
- 在applicationContext.xml 中配置织入关系
- 测试代码
-
XML配置AOP详解
-
切点表达式的写法
表达式语法 :
< aop:before method=“before” pointcut=“execution(public void aop.aspectj.Target1.save())” />
< aop:before method=“before” pointcut=“execution(* aop.aspectj..(…))”/>- 访问修饰符可以省略
- 返回值类型,包名,类名,方法名可以使用星号 * 代表任意
- 包名与类名之间一个点,代表当前包下的类,两个点…表示当前包及其子包下的类
- 参数列表可以使用两个点…表示任意个数,任意类型的参数列表
-
通知类型 :
- 通知的配置语法 : < aop :通知类型 method = “切面类中方法名” pointcut = “切点表达式”/ >
- 前置通知 : < aop : before> : 用于配置前置通知,指定增强的方法在切入点方法之前执行
- 后置通知 : < aop : after-returning> : 用于配置后置通知,指定增强的方法在切入点方法之后执行
- 环绕通知 : < aop : around> : 用于配置环绕通知,指定增强的方法在切入点方法之前和之后都执行
- 异常抛出通知 : < aop:throwing> : 用于配置异常抛出通知,指定增强的方法在出现异常时执行
- 最终通知 : < aop : after> : 用于配置最终通知,无论增强方法的执行是否出现异常都会执行
- 通知的配置语法 : < aop :通知类型 method = “切面类中方法名” pointcut = “切点表达式”/ >
-
<?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 id="target" class="aop.aspectj.Target1"/>
<!-- 切面对象-->
<bean id="myAspect" class="aop.aspectj.MyAspect"/>
<!-- 配置织入,告诉spring框架,那些方法(切点), 需要进行那些增强-->
<aop:config>
<!-- 声明切面-->
<aop:aspect ref="myAspect">
<!--切面 : 切点和通知
<aop:before method="before" pointcut="execution(public void aop.aspectj.Target1.save())"></aop:before>
<aop:before method="before" pointcut="execution(* aop.aspectj.*.*(..))"></aop:before>
<aop:after-returning method="afterreturn" pointcut="execution(* aop.aspectj.*.*(..))"></aop:after-returning>
<aop:around method="arround" pointcut="execution(* aop.aspectj.*.*(..))"></aop:around>
<aop:after-throwing method="afterThrowing" pointcut="execution(* aop.aspectj.*.*(..))"></aop:after-throwing>
<aop:after method="after" pointcut="execution(* aop.aspectj.*.*(..))"></aop:after>-->
<!-- 抽取切点表达式-->
<aop:pointcut id="myPointcut" expression="execution(* aop.aspectj.*.*(..))"/>
<aop:around method="arround" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
基于注解的AOP
- 快速入门
- 导入AOP相关坐标 ( spring-context, aspectjweaver)
- 创建目标接口和目标类
- 创捷切面类(内部有增强方法)
- 将目标类和切面类的对象创建权交给Spring
- 在切面类中使用注解配置织入关系
- 在配置文件中开启组件扫描和AOP的自动代理
- 测试代码
- 注解配置AOP详解
- 前置通知 : @Before : 用于配置前置通知,指定增强的方法在切入点方法之前执行
- 后置通知 : @AfterReturning : 用于配置前置通知,指定增强的方法在切入点方法之后执行
- 前置通知 : @Arround : 用于配置环绕通知,指定增强的方法在切入点方法之前和之后执行
- 前置通知 : @AfterThrowing : 用于配置异常抛出通知,指定增强的方法在出现异常时执行
- 前置通知 : @After : 用于配置最终通知,无论增强的方法在执行是否有异常都会执行
注解抽取: execution(* aop.aspectj..(…))
@Around("pointcut()")
//proceeding joinpoint 正在执行的连接点==切点
public Object arround(ProceedingJoinPoint proceedingJointPoint) throws Throwable {
System.out.println("我是around");
Object proceed = proceedingJointPoint.proceed();
System.out.println("我都要增强....");
return proceed;
}
public void afterThrowing(){
System.out.println("异常增强....");
}
@After("MyAspect2.pointcut()")
public void after(){
System.out.println("最终增强...." );
}
@Pointcut("execution(* aop.anno.*.*(..))")
public void pointcut(){}
MyAspect2类( 切面类 ) :
package aop.anno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component("myAspect2")
@Aspect // 标注当前MyAspect2是一个切面类
public class MyAspect2 {
@Before("execution(* aop.anno.*.*(..))")
public void before(){
System.out.println("前置增强....");
}
public void afterreturn(){
System.out.println("后置增强....");
}
//proceeding joinpoint 正在执行的连接点==切点
public Object arround(ProceedingJoinPoint proceedingJointPoint) throws Throwable {
System.out.println("我是around");
Object proceed = proceedingJointPoint.proceed();
System.out.println("我都要增强....");
return proceed;
}
public void afterThrowing(){
System.out.println("异常增强....");
}
public void after(){
System.out.println("最终增强...." );
}
}
Target2类( 目标类):
package aop.anno;
import org.springframework.stereotype.Component;
@Component("target2")
public class Target2 implements TargetInterface2{
public void save() {
System.out.println("save running....");
//int i = 1/0;
}
}
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
">
<!--组件扫描-->
<context:component-scan base-package="aop"/>
<aop:aspectj-autoproxy/>
</beans>