Spring(四)--------AOP、整合Mybatis、声明式事务
11、AOP(重点)
11.1 什么是AOP
-
总的来说:AOP是在不影响原始业务类的情况下,对代码进行动态的增强
-
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
11.2 AOP在Spring中的作用
- 提供声明式事务;允许用户自定义切面
以下名词需要了解下:
- 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
- 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
- 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
- 目标(Target):被通知对象。
- 代理(Proxy):向目标对象应用通知之后创建的对象。
- 切入点(PointCut):切面通知 执行的 “地点”的定义。
- 连接点(JointPoint):与切入点匹配的执行点。
5类Advice
SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:
通知类型 | 连接点 | 实现接口 |
---|---|---|
前置通知 | 方法前 | org.springframework.aop.MethodBeforeAdvice |
后置通知 | 方法后 | org.springframework.aop.AfterReturningAdvice |
环绕通知 | 方法前后 | org.aopalliance.intercept.MethodInterceptor |
异常抛出通知 | 方法抛出异常 | org.springframework.aop.ThrowsAdvice |
引介通知 | 类中增加新的方法属性 | org.springframework.aop.IntroductionInterceptor |
各类增强
-
前置增强:org.springframework.aop.BeforeAdvice代表前置增强,因为Spring只支持
方法级的增强,所以MethodBeforeAdvice是目前可的的前置增强,表示在目标方法执行前实施增强,而BeforeAdvice是为了将来版本扩展需要而定义的; -
后置增强:org.springframework.aop.AfterReturningAdvice代表后增强,表示在目标
方法执行后实施增强; -
环绕增强:org.aopalliance.intercept.MethodInterceptor代表环绕增强,表示在目标
方法执行前后实施增强; -
异常抛出增强:org.springframework.aop.ThrowsAdvice代表抛出异常增强,表示在目标方法抛出异常后实施增强;
-
引介增强:org.springframework.aop.IntroductionInterceptor代表引介增强,表示在
目标类中添加一些新的方法和属性; -
这些增强接口都有一些方法,通过实现这些接口方法,在接口方法中这义横切逻辑,就可以将它们织入到目标类的方法的相应连接点的位置。
-
即 Aop 在 不改变原有代码的情况下 , 去增加新的功能 .
约束
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
11.3 使用Spring实现AOP
- 主要SpringAPI接口实现
【重点】使用AOP织入,需要导入一个依赖包
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
第一种方式:使用Spring的API接口
- 编写UserService接口
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
- 编写UserServiceImpl实现类
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("更新了一个用户");
}
@Override
public void query() {
System.out.println("查询了一个用户");
}
}
- 编写前置增强类
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法 args:被调用的方法的参数 target:目标对象
//类比动态代理ProxyInvocationHandler
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
//target.getClass().getName() 类名 method.getName()方法名字
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
- 编写后置增强类
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
//method:被调用的方法
//args:被调用的方法的对象的参数
//target:被调用的目标对象
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
- 编写applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.zmt.service.UserServiceImpl"/>
<bean id="log" class="com.zmt.log.Log"/>
<bean id="afterLog" class="com.zmt.log.AfterLog"/>
<!--方式一:使用原生Spring API接口-->
<!--配置AOP:需要导入AOP的约束-->
<aop:config>
<!--切入点 expression:表达式(要执行的位置:修饰词,返回值,类名,方法名,参数),切入点可以有多个-->
<!--com.zmt.service.UserServiceImpl.*(..)
第一个*:返回值类型任意
第二个*:代表类中所有方法 (..):代表任意数量参数-->
<aop:pointcut id="pointcut" expression="execution(* com.zmt.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<!--advice-ref:advice引用了哪个类 pointcut-ref:插入到哪个切入点-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
- 测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理,代理的是接口,不是实现类
UserService userService = context.getBean("userService", UserService.class);
userService.query();
}
}
- Spring的Aop就是将公共的业务 (日志 , 安全等) 和领域业务结合起来 , 当执行领域业务时 , 将会把公共业务加进来。实现公共业务的重复利用 . 领域业务更纯粹 , 程序猿专注领域业务 , 其本质还是动态代理
第二种方式:自定义类来实现AOP
-
主要是切面定义
-
自定义切入类[增强类](相当于第一种方式的Log和afterLog)
public class DiyPointCut {
public void before(){
System.out.println("================方法执行前===============");
}
public void after(){
System.out.println("================方法执行后===============");
}
}
- 配置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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class=