介绍&步骤
视频教程: https://www.bilibili.com/video/BV1WZ4y1P7Bp?p=121
官方笔记链接:https://pan.baidu.com/s/1dnL5hwOPHPMNgb81yzQIOQ
提取码:2022
目录:
项目目录结构:
介绍
作用和优势
- 作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强
- 优势:减少重复代码,提高开发效率,并且便于维护
实现
实际上,AOP 的底层是通过 Spring 提供的的动态代理技术实现的。在运行期间,Spring通过动态代理技术动态
的生成代理对象,代理对象方法执行时进行增强功能的介入,在去调用目标对象的方法,从而完成功能的增强
1.两种代理技术
常用的动态代理技术
- JDK 代理 : 基于接口的动态代理技术 (必须要有接口) 目标对象和代理对象是
兄弟关系
- cglib 代理:基于父类的动态代理技术 目标对象和代理对象不是兄弟关系, 类似
父子关系
1. jdk代理
三个角色 目标对象 目标接口对象 代理对象
加上增强方法Advice
1.1 目标接口TargetInterface
// 目标接口
public interface TargetInterface {
public void save();
}
1.2 目标对象(实现目标接口)
需要被增强的方法
// 目标对象
public class Target implements TargetInterface{
// 需要被增强的方法
public void save() {
System.out.println("save running....");
}
}
1.3 通知Advice
(用于增强的方法)
// 用于增强的方法
public class Advice {
public void before(){
System.out.println("前置增强代码.....");
}
public void afterReturning(){
System.out.println("后置增强代码......");
}
}
1.4 代理测试方法
public class ProxyTest {
public static void main(String[] args) {
// 目标对象(需要增强的方法对象)
final Target target = new Target();
// 用于增强的对象
final Advice advice = new Advice();
// 生成代理对象 三个参数 目标对象的类加载器, 目标对象相同的接口字节码对象数组 调用代理对象的任何方法是 实质执行的都是invoke方法
// jdk代理对象是基于接口的 proxy代理对象和target目标对象是兄弟关系 所以不能使用Target来接受proxy
TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
advice.before(); // 执行前置增强
Object invoke = method.invoke(target, args);
advice.afterReturning(); // 执行后置增强
return invoke;
}
});
//调用代理对象的方法
proxy.save();
}
}
2. cglib 代理
两个角色 目标对象 代理对象
加上增强方法Advice
2.1 导入坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
2.2 目标对象
public class Target {
public void save() {
System.out.println("save running....");
}
}
2.3 增强方法
public class Advice {
public void before(){
System.out.println("cglib前置增强代码.....");
}
public void afterReturning(){
System.out.println("cglib后置增强代码......");
}
}
2.4 代理对象测试代码
// 利用cglib进行aop增强
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() {
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
//执行前置
advice.before();
//执行目标
Object invoke = method.invoke(target);
//执行后置
advice.afterReturning();
return invoke;
}
});
//4.创建对象对象
Target proxy = (Target) enhancer.create();
proxy.save();
}
3. AOP 相关概念
3.1 AOP 开发明确的事项
1.需要编写的内容
- 编写核心业务代码(目标类的目标方法)
- 编写切面类,切面类中有通知(增强功能方法)
- 在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合
- AOP 技术实现的内容
Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的
代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。 - AOP 底层使用哪种代理方式
在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。
3.2 要点
- aop:面向切面编程
- aop底层实现:基于JDK的动态代理 和 基于Cglib的动态代理
- aop的重点概念:
Pointcut(切入点):被增强的方法
Advice(通知/ 增强):封装增强业务逻辑的方法
Aspect(切面):切点+通知
Weaving(织入):将切点与通知结合的过程 - 开发明确事项:
谁是切点(切点表达式配置)
谁是通知(切面类中的增强方法)
将切点和通知进行织入配置
2. 基于 XML 的 AOP 开发
2.1 快速入门
① 导入 AOP 相关坐标
② 创建目标接口和目标类(内部有切点)
③ 创建切面类(内部有增强方法)
④ 将目标类和切面类的对象创建权交给 spring
⑤ 在 applicationContext.xml 中配置织入关系
⑥ 测试代码
2.1.1 导入坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--轻量化的框架配置aop 推荐使用-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
2.1.2 创建目标接口和目标类(内部有切点)
目标接口
public interface TargetInterface {
void save();
}
目标类
public class Target implements TargetInterface {
@Override
public void save() {
System.out.println("save running....");
// int i = 1/0;
}
}
2.1.3 创建切面类(内部有增强方法)
public class MyAspect {
public void before(){
System.out.println("前置增强....");
}
public void afterRunning(){
System.out.println("后置增强.....");
}
public void wjg(){
System.out.println("wjg增强.....");
}
public void afterThrowing(){
System.out.println("异常增强抛出.....");
}
public void after(){
System.out.println("最终增强........");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕前增强....");
Object proceed = pjp.proceed();
//切点方法
System.out.println("环绕后增强");
return proceed;
}
}
2.1.4 applicationContext.xml配置目标类和切面类的Bean
applicationContext.xml
配置
<!-- 目标对象 -->
<bean id="target" class="review.aop.Target"></bean>
<!-- 切面对象 -->
<bean id="myAspect" class="review.aop.MyAspect"></bean>
2.1.5 在 applicationContext.xml 中配置织入关系
applicationContext.xml
配置
<!--配置织入:告诉spring框架哪些方法(切点)需要进行哪些增强(前置、后置..-->
<aop:config>
<!-- 声明切面-->
<aop:aspect ref="myAspect">
<!-- 抽取切点表达式 -->
<!-- <aop:pointcut id="myPointCut" expression="execution(public void review.aop.Target.save())"/>-->
<aop:pointcut id="myPointCut" expression="execution(* review.aop.*.*())"/>
<aop:before method="before" pointcut-ref="myPointCut"/>
<aop:around method="around" pointcut-ref="myPointCut"/>
<aop:after-returning method="afterRunning" pointcut-ref="myPointCut"/>
<aop:after method="after" pointcut-ref="myPointCut"/>
<!-- <aop:after-throwing method="afterThrowing" pointcut-ref="myPointCut"/>-->
<!-- <aop:before method="before" pointcut="execution(public void review.aop.Target.save())"/>-->
</aop:aspect>
</aop:config>
到这里applicaitonContext.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="review.aop.Target"></bean>
<!-- 切面对象 -->
<bean id="myAspect" class="review.aop.MyAspect"></bean>
<!--配置织入:告诉spring框架哪些方法(切点)需要进行哪些增强(前置、后置..-->
<aop:config>
<!-- 声明切面-->
<aop:aspect ref="myAspect">
<!-- 抽取切点表达式 -->
<!-- <aop:pointcut id="myPointCut" expression="execution(public void review.aop.Target.save())"/>-->
<aop:pointcut id="myPointCut" expression="execution(* review.aop.*.*())"/>
<aop:before method="before" pointcut-ref="myPointCut"/>
<aop:around method="around" pointcut-ref="myPointCut"/>
<aop:after-returning method="afterRunning" pointcut-ref="myPointCut"/>
<aop:after method="after" pointcut-ref="myPointCut"/>
<!-- <aop:after-throwing method="afterThrowing" pointcut-ref="myPointCut"/>-->
<!-- <aop:before method="before" pointcut="execution(public void review.aop.Target.save())"/>-->
</aop:aspect>
</aop:config>
</beans>
一个简单逻辑
2.1.6 测试代码(SpringJunitTest)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml") //加载配置容器 不加载注入不了
public class AopTest {
@Autowired
private TargetInterface target;
@Test
public void hello() {
System.out.println(target);
}
@Test
public void test1(){
target.save();
}
}
运行test1结果
2.2 XML 配置 AOP 详解
2.2.1 切点表达式的写法
表达式语法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
```~~删除线格式~~
- 访问修饰符可以省略
- 返回值类型、包名、类名、方法名可以使用星号* 代表任意
- 包名与类名之间一个点 . 代表当前包下的类,两个点 ..表示当前包及其子包下的类
- 参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表
```java
execution(public void com.itheima.aop.Target.method())
execution(void com.itheima.aop.Target.*(..))
execution(* com.itheima.aop.*.*(..)) // aop 包下的任意类的任意方法
execution(* com.itheima.aop..*.*(..)) // aop 子包下的任意类的任意方法
execution(* *..*.*(..))
常用的
execution(* com.itheima.aop.*.*(..))
2.2.2通知的类型
2.2.3 切点表达式的抽取
<aop:config>
<!--引用myAspect的Bean为切面对象-->
<aop:aspect ref="myAspect">
<aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:before method="before" pointcut-ref="myPointcut"></aop:before>
</aop:aspect>
</aop:config>
逻辑
3.基于注解的 AOP 开发
3.1 快速入门
基于注解的aop开发步骤:
① 创建目标接口和目标类(内部有切点)
② 创建切面类(内部有增强方法)
③ 将目标类和切面类的对象创建权交给 spring
④ 在切面类中使用注解配置织入关系
⑤ 在配置文件中开启组件扫描和 AOP 的自动代理
⑥ 测试
3.1.1 创建目标接口和目标类
目标接口类
public interface TargetInterface {
void save();
}
目标类
@Component("target") // 配置bean 存放在spring容器中 id target
public class Target implements TargetInterface {
public void save(){
System.out.println("save running......");
}
}
3.1.2 创建切面类(内部有增强方法)
@Component("myAspect") // 在spring 容器中配置bean id为myAspect
@Aspect //标注为切面 织入
public class MyAspect {
@Before("pointcut()")
public void before(){
System.out.println("前置增强.....");
}
public void wjg(){
System.out.println("wjg增强.....");
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强.....");
Object proceed = pjp.proceed();
System.out.println("环绕后增强");
return proceed;
}
@AfterReturning("pointcut()")
public void afterReturning(){
System.out.println("后置增强.......");
}
// @AfterThrowing("pointcut()")
public void afterThrowing(){
System.out.println("异常增强抛出.......");
}
// @After("MyAspect.pointcut()")
// @After("pointcut())")
public void after(){
System.out.println("最终增强");
}
@Pointcut("execution(* review.anno.*.*(..))")
public void pointcut(){
}
}
3.1.3 将目标类和切面类的对象创建权交给 spring(已配置)
@Component("myAspect") // 在spring 容器中配置bean id为myAspect
@Component("target") // 配置bean 存放在spring容器中 id target
3.1.4 在切面类中使用注解配置织入关系
告知spring容器这是切面
@Aspect //标注为切面 织入
切点表达式抽取
@Pointcut("execution(* review.anno.*.*(..))")
public void pointcut(){
}
前置增强
@Before("pointcut()") //pointcut的方法名称 切点表达式
// @Before("execution(* review.anno.*.*(..))") // 括号里面是切点
public void before(){
System.out.println("前置增强.....");
}
3.1.5 在配置applicationContext-anno.xml
文件中开启组件扫描和 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
">
<!-- 目标对象 已经注入了 不需要配置 -->
<!-- <bean id="target" class="review.anno.Target"></bean>-->
<!-- 组件扫描 相当于自动配置bean标签 如果没有配置的话 就不能进行自动注入 -->
<context:component-scan base-package="review.anno"/>
<!-- aop自动代理 用于aop的自动代理 不加这个使用不了增强方法 -->
<aop:aspectj-autoproxy/>
</beans>
3.1.6 测试代码 SpringTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AnnoTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.save();
}
}
3.1.7 知识要点
注解aop开发步骤
① 使用@Aspect标注切面类
② 使用@通知注解标注通知方法
③ 在配置文件中配置aop自动代理
<!-- aop自动代理 用于aop的自动代理 不加这个使用不了增强方法 -->
<aop:aspectj-autoproxy/>