目录
1.1什么是AOP
AOP为AspectOriented Programming的缩写,意思为面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率
1.2AOP的作用及其优势
作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强
优势:减少重复代码,提高开发效率,并且便于维护
1.3AOP的底层实现
实际上,AOP的底层是通过Spring提供的的动态代理技术实现的。在运行期间,Spring通过动态代理技术动态的生成代理对象,代理对象方法执行时进行增强功能的介入,再去调用目标对象的方法,从而完成功能的增强。
1.4AOP的动态代理技术
常用的动态代理技术
JDK代理:基于接口的动态代理技术
cglib代理:基于父类的动态代理技术
1.5代码实现JDK动态代理和cglib动态代理
JDK动态代理
接口
package com.hzw.proxy.jdk; public interface TargetInterface { public void save(); }
实现类
package com.hzw.proxy.jdk;
public class Target implements TargetInterface {
@Override
public void save() {
System.out.println("鸡腿.....");
}
}
代理类
package com.hzw.proxy.jdk;
public class Advice {
public void before(){
System.out.println("前置增强....加一碗饭");
}
public void after(){
System.out.println("后置增强.....加个鸡蛋");
}
}
test
package com.hzw.proxy.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class RiceTest {
public static void main(String[] args) {
//增强对象
final Advice advice = new Advice();
//目标对象
final Target target = new Target();
TargetInterface proxyInstance=(TargetInterface) Proxy.newProxyInstance(
//目标对象类加载器
target.getClass().getClassLoader(),
//目标对象接口数组
target.getClass().getInterfaces(),
//invole
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//前置增强
advice.before();
//执行目标方法
Object invoke = method.invoke(target, args);
//后置方法
advice.after();
return invoke;
}
}
);
//调用代理对象方法
proxyInstance.save();
}
}
cglib动态代理
代理类
package com.hzw.proxy.clgb;
public class Advice {
public void before(){
System.out.println("前置增强....加一碗饭");
}
public void after(){
System.out.println("后置增强.....加个鸡蛋");
}
}
package com.hzw.proxy.clgb;
public class Target {
public void save() {
System.out.println("鸡腿.....");
}
}
其他类如上所同
package com.hzw.proxy.clgb;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class RiceTest {
public static void main(String[] args) {
//增强对象
final Advice advice = new Advice();
//目标对象
final Target target = new Target();
//1.设置增强器
Enhancer enhancer = new Enhancer();
//2.设置父类
enhancer.setSuperclass(Target.class);
//3.设置回调
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
//前置增强
advice.before();
//执行目标对象
Object invoke = method.invoke(target, args);
//后置增强
advice.after();
return invoke;
}
});
//创建代理对象
Target proxy= (Target)enhancer.create();
proxy.save();
}
}
1.6AOP相关概念
Spig的AOP实现底层就是对上面的动态代理的代码进行了封装,封装后我们只需要对需要关注的部分进行代码编写,并通过配置的方式完成指定目标的方法增强。
在正式讲解AOP的操作之前,我们必须理解AOP的相关术语,常用的术语如下:
。Target(目标对象):代理的目标对象
·Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类
Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(能进行增强的方法)
●Pointcut(切入点):所谓切入点是指我们要对哪些joinpoint进行拦截的定义
●Advice(G通知/增强):所谓通知是指拦截到Joinpoint.之后所要做的事情就是通知
·Aspect(切面):是切入点和通知(引介)的结合
Weaving(织入):是指把增强应用到目标对像来创建新的代理对象的过程。spring:采用动态代理织入,而Aspect采用编译期织入和类装载期织入(配置)
1.7AOP开发明确的事项
1.需要编写的内容
编写核心业务代码(目标类的目标方法)
编写切面类,切面类中有通知(增强功能方法)
在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结器
2.AOP技术实现的内容
Spig框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的
代理对象,根据通知类别,在代理对像的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。
3.AOP底层使用哪种代理方式
在spring中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。
1.8基于XML的AOP开发
1.知识要点
●aop:面向切面编程
aop底层实现:基于JDK的动态代理和基于Cglib的动态代理
aop的重点概念:
Pointcut(切入点):被增强的方法
Advice(通知/增强):封装增强业务逻辑的方法
Aspect(切面):切点+通知
Veaving(织入):将切点与通知结合的过程
开发明确事项:
谁是切点(切点表达式配置)
谁是通知(切面类中的增强方法)
将切点和通知进行织入配置
2.执行开发流程
1.导入AOP相关坐标
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!--aop配置--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.5.RELEASE</version> </dependency> </dependencies>
2.创建目标接口和目标类
public interface TargetInterface { public void save(); }
public class Target implements TargetInterface { @Override public void save() { System.out.println("鸡腿....."); } }
3.创建切面类
public class Aspect { public void before(){ System.out.println("前置增强....加一碗饭"); } }
4.将目标类和切面类的创建交给spring(配置applicationContext.xml)
5.在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"
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="com.hzw.aop.Target"></bean>
<!--切面对象-->
<bean id="aspect" class="com.hzw.aop.Aspect"></bean>
<!---->
<aop:config>
<!--声明切面-->
<aop:aspect ref="aspect">
<!--切面,通知+切点-->
<aop:before method="before" pointcut="execution(public void com.hzw.aop.Target.save())"></aop:before>
</aop:aspect>
</aop:config>
</beans>
6.测试
1.8.1基于XML的AOP详讲
知识要点
1.切点表达式的写法
2.通知的类型
public class Target implements TargetInterface {
@Override
public void save() {
System.out.println("鸡腿.....");
}
@Override
public void eat() {
System.out.println("吃炸鸡.....");
}
}
切面类
package com.hzw.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class Aspect {
public void before() {
System.out.println("前置增强....加一碗饭");
}
public void afterReturing() {
System.out.println("后置增强.....加个鸡蛋");
}
//ProceedingJoinPoint 切点
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕前增强.....加棵大白菜");
Object proceed = point.proceed();
System.out.println("环绕后增强.....加肥肠");
return proceed;
}
public void afterThrowing(){
System.out.println("异常抛出增强.....不加菜");
}
public void after(){
System.out.println("最终增强.....加烤全羊加火腿加烤鸭加炸鸡全桶套餐加可乐加啤酒...等等");
}
}
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"
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="com.hzw.aop.Target"></bean>
<!--切面对象-->
<bean id="aspect" class="com.hzw.aop.Aspect"></bean>
<!---->
<aop:config>
<!--声明切面-->
<aop:aspect ref="aspect">
<!--切面,通知+切点-->
<!--<aop:before method="before" pointcut="execution(public void com.hzw.aop.Target.save())"></aop:before>-->
<!--<aop:before method="before" pointcut="execution(* com.hzw.aop.*.*(..))"/>-->
<!--<aop:after-returning method="afterReturing" pointcut="execution(* com.hzw.aop.*.*(..))"/>-->
<!--<aop:around method="around" pointcut="execution(* com.hzw.aop.*.*(..))"/>-->
<!--<aop:after-throwing method="afterThrowing" pointcut="execution(* com.hzw.aop.*.*(..))"/>-->
<aop:after method="after" pointcut="execution(* com.hzw.aop.*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
1.8.2基于注解的AOP开发
1.基于注解的aop开发步骤:
①创建目标接口和目标类(内部有切点)
public interface TargetInterface {
public void save();
public void eat();
}
package com.hzw.controller;
import org.springframework.stereotype.Component;
@Component("target")
public class Target implements TargetInterface {
@Override
public void save() {
//int i=1/0;
System.out.println("鸡腿.....");
}
@Override
public void eat() {
System.out.println("吃炸鸡.....");
}
}
②创建切面类(内部有增强方法)
2.注解通知类型和切点表达式
package com.hzw.controller;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component("myAspect")
@Aspect
public class MyAspect {
//@Before(value = "execution(* com.hzw.controller.*.*(..))")
public void before() {
System.out.println("前置增强....加一碗饭");
}
@AfterReturning("execution(* com.hzw.controller.*.*(..))")
public void afterReturing() {
System.out.println("后置增强.....加个鸡蛋");
}
//ProceedingJoinPoint 切点
@Around("execution(* com.hzw.controller.*.*(..))")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕前增强.....加棵大白菜");
Object proceed = point.proceed();
System.out.println("环绕后增强.....加肥肠");
return proceed;
}
@AfterThrowing("execution(* com.hzw.controller.*.*(..))")
public void afterThrowing() {
System.out.println("异常抛出增强.....不加菜");
}
@After("execution(* com.hzw.controller.*.*(..))")
public void after() {
System.out.println("最终增强.....加烤全羊加火腿加烤鸭加炸鸡全桶套餐加可乐加啤酒...等等");
}
}
③将目标类和切面类的对象创建权交给spring(@Component())
④在切面类中使用注解配置织入关系(注解@Aspect)
⑤在配置文件中开启组件扫描和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"
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 http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--组件扫描-->
<context:component-scan base-package="com.hzw.controller"/>
<!--aop自动代理-->
<aop:aspectj-autoproxy/>
</beans>
⑥测试