简述
注解方式实现AOP → 注解实现
AOP:
- 面向切面编程,
- AOP是一种编程范式,Spring将AOP思想引入到框架中,
- 使用AOP可以对某个模块的功能进行增强,
- SpringAOP底层使用的是动态代理来实现,
- 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率,
- AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码
- 学习AOP的原因:
在OOP中要实现一个功能的增强,可以继承类或实现一个接口来完成,这样就会使得代码的耦合度增强;
AOP思想通过无缝切入来实现,无需继承类或实现接口,从而达到解耦。
可以在不修改源代码的前提下,对程序功能进行增强!(解耦合,强功能)
AOP的术语
- 通知的类型:
@Before -- 前置通知(在目标类的方法执行之前执行)
@AfterReturing -- 后置通知(在目标类的方法正常执行后的通知, 程序出现异常, 则不会执行)
@After -- 最终通知(在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行)
@Around -- 环绕通知(在目标类方法的执行前后执行)
@AfterThrowing -- 异常抛出通知(在目标类方法执行出现异常时, 才会调用该通知)
- 相关术语
Joinpoint(连接点) -- 连接点是指那些被拦截到的点。在spring中,目标类的所有方法都可当做是一个连接点
Pointcut(切入点) -- 切入点是指我们要对哪些Joinpoint进行拦截的定义
Advice(通知/增强) -- 通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
Introduction(引介) -- 引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field
Target(目标对象) -- 代理的目标对象
Weaving(织入) -- 是指把增强应用到目标对象来创建新的代理对象的过程
Proxy(代理) -- 一个类通过AOP织入增强后,就会产生一个结果代理类
Aspect(切面) -- 是切入点与通知的结合
XML配置方式实现Spring AOP
使用 Maven 来完成
- 完整构建图
- pom.xml
<!-- 导入两个依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
</dependencies>
- 导入依赖后的jar包
- MyService 接口
package com.zlk.service;
public interface MyService {
public void before();
public void after();
public void around();
public void afterReturning();
public void afterThrowing();
}
- MyServiceImpl 类 (MyService 接口的实现类)
package com.zlk.service.impl;
import org.springframework.stereotype.Service;
import com.zlk.service.MyService;
@Service("myService") // 等价于==> <bean id="myService" class="com.zlk.service.impl.MyServiceImpl"></bean>
public class MyServiceImpl implements MyService {
@Override
public void before() {
System.out.println("before message");
}
@Override
public void after() {
// int a = 10 / 0;
System.out.println("after message");
}
@Override
public void around() {
System.out.println("around message");
}
@Override
public void afterReturning() {
// int a = 10 / 0;
System.out.println("afterReturning message");
}
@Override
public void afterThrowing() {
int a = 10 / 0;
System.out.println("afterThrowing message");
}
}
- MyAop 切面类
package com.zlk.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAop {
// 切入点
public void anyMethod() { }
// 前置通知
// 在目标类的方法执行之前执行
public void beforeAdvice() {
System.out.println("前置通知!");
}
// 最终通知
// 在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行
public void afterAdvice() {
System.out.println("最终通知!");
}
// 后置通知
// 方法正常执行后的通知, 程序出现异常, 则不会执行
public void afterReturningAdvice() {
System.out.println("后置通知!");
}
// 环绕通知
// 在目标类方法的执行前后执行
public void aroundAdvice(ProceedingJoinPoint joinPoint) {
System.out.println("环绕通知 - 前");
try {
joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("环绕通知 - 后");
}
// 异常抛出通知
// 在目标类方法执行出现异常时, 才会调用该通知
public void afterThrowingAdvice() {
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: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">
<!-- 开启自动代理, 注解方式才需要开启 -->
<!-- <aop:aspectj-autoproxy /> -->
<!-- 配置切面类 bean -->
<bean id="myAop" class="com.zlk.aop.MyAop"></bean>
<!-- 配置目标类 bean -->
<bean id="myService" class="com.zlk.service.impl.MyServiceImpl"></bean>
<!-- aop 配置 -->
<aop:config>
<!-- 自定义切入点 -->
<aop:pointcut expression="execution(* *..*.before(..))" id="anyMethod"/>
<aop:aspect ref="myAop">
<!-- 前置通知 -->
<!-- 测试自定义切入点 -->
<aop:before method="beforeAdvice" pointcut-ref="anyMethod"/>
<!-- 最终通知 -->
<aop:after method="afterAdvice" pointcut="execution(* *..*.after(..))"/>
<!-- 后置通知 -->
<aop:after-returning method="afterReturningAdvice" pointcut="execution(* *..*.afterReturning(..))"/>
<!-- 环绕通知 -->
<aop:around method="aroundAdvice" pointcut="execution(* *..*.around(..))"/>
<!-- 异常抛出通知 -->
<aop:after-throwing method="afterThrowingAdvice" pointcut="execution(* *..*.afterThrowing(..))"/>
</aop:aspect>
</aop:config>
</beans>
- MyTest 测试类
package com.zlk.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zlk.service.MyService;
public class MyTest {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
MyService myService = (MyService) beanFactory.getBean("myService");
// 测试前置通知
myService.before();
// 测试最终通知
// myService.after();
// 测试后置通知
// myService.afterReturning();
// 测试环绕通知
// myService.around();
// 测试异常抛出通知
// myService.afterThrowing();
}
}