Spring实现AOP的三大方式
AOP(Aspect Oriented Programming)面向切面编程,通过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术。通俗点,就是在不改变系统原本业务功能的前提下,对系统的功能进行横向扩展。
一、AOP的相关概念
- 横切关注点:对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
- Aspect(切面):通常是一个类,里面可以定义切入点和通知
- JointPoint(连接点):程序执行过程中明确的点,一般是方法的调用。被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
- Advice(通知):AOP在特定的切入点上执行的增强处理,有before(前置),after(后置),afterReturning(最终),afterThrowing(异常),around(环绕)
- Pointcut(切入点):就是带有通知的连接点,在程序中主要体现为书写切入点表达式
- weave(织入):将切面应用到目标对象并导致代理对象创建的过程
- introduction(引入):在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
- AOP代理(AOP Proxy):AOP框架创建的对象,代理就是目标对象的加强。Spring中的AOP代理可以使JDK动态代理,也可以是CGLIB代理,前者基于接口,后者基于子类
- 目标对象(Target Object): 包含连接点的对象。也被称作被通知或被代理对象。
二、使用AOP的三大方式
- 方式一:使用原生Spring API接口
- 方式二:自定义切面类
- 方式三:注解方式
方式一:使用原生Spring API接口
创建UserService接口,包含4个方法,并创建UserServiceImpl类实现该接口。
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void select() {
System.out.println("查询了一个用户");
}
public void update() {
System.out.println("修改了一个用户");
}
}
创建Log类,实现MethodBeforeAdvice接口,在这里可以进行一些自定义操作,比如向控制台输出一句话,还可以利用反射机制获得该方法的一些信息,必须方法名等,这是另外两大方法所不具备的优点,该切面方法会放在切入点方法调用之前调用。
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
创建AfterLog类,实现AfterReturningAdvice接口,在这里可以进行一些自定义操作,比如向控制台输出一句话,还可以利用反射机制获得该方法的一些信息,必须方法名等,这是另外两大方法所不具备的优点,该切面方法会放在切入点方法调用之前调用。
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
在xml配置文件中的标签中应加上对应的aop地址,创建相应bean,对aop进行配置,并导入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.kuang.service.UserServiceImpl"/>
<bean id="log" class="com.kuang.log.Log"/>
<bean id="afterLog" class="com.kuang.log.AfterLog"/>
<!-- 方式一:使用原生Spring API接口-->
<!-- 配置aop:需要导入aop的约束-->
<aop:config>
<!-- 切入点:execution:表达式,execution(要执行的位置)-->
<aop:pointcut id="poinicut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加 -->
<aop:advisor advice-ref="log" pointcut-ref="poinicut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="poinicut"/>
</aop:config>
com.kuang.service.UserServiceImpl.(…)指切入点为UserServiceImpl类中的所有方法。
运行截图:
com.kuang.service.UserServiceImpl的add被执行了
增加了一个用户
执行了add方法,返回结果为:null
方式二:自定义切面类
创建自定义切面类DiyPointCut,里面包含一些自定义的切面方法。
public class DiyPointCut {
public void before(){
System.out.println("===========方法执行前==========");
}
public void after(){
System.out.println("===========方法执行后==========");
}
}
在applicationContext.xml文件中的aop配置为:
<!-- 方式二:自定义类-->
<bean id="diy" class="com.kuang.diy.DiyPointCut"/>
<aop:config>
<!-- 自定义切面,ref要引用的类-->
<aop:aspect ref="diy">
<!--切入点 -->
<aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--通知 -->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
运行结果
=========== 方法执行前 ==========
增加了一个用户
=========== 方法执行后==========
方式三:注解方式
建立一个切面方法类(AnnotationPointCut),里面包含一些切面方法,比如before、after、aroud等。
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//方式三:使用注解方式实现AOP
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("==========方法执行前==========");
}
@After("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("==========方法执行后==========");
}
@Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable{
System.out.println("环绕前");
Object proceed=jp.proceed();
System.out.println("环绕后");
}
}
在applicationContext.xml配置文件中对aop进行配置
<!-- 方式三:注解方式-->
<bean id="annotationPointCut" class="com.kuang.diy.AnnotationPointCut"/>
<!--开启注解支持 -->
<aop:aspectj-autoproxy/>
运行结果
环绕前
========== 方法执行前==========
增加了一个用户
环绕后
========== 方法执行后==========
这里注意aroud与before、after方法的执行顺序:
环绕前—>方法执行前—>增加了一个用户—>环绕后—>方法执行后
总结
- 使用原生的spring AIP接口,代码较为复杂,但可以在切面方法中利用反射机制获得切入点方法的相关信息,比如获得切入点方法的方法名。
- 使用自定义切面类的方法,简单易懂。
- 使用注解的方式,方便便捷,大大简化了xml配置文件中aop代码,只需要开启注解即可
- 最重要的是:AOP思想可以与动态代理相结合,在本例中有所体现,UserServiceImpl类实现了UserService接口,我们可以在UserServiceImpl类中对UserService接口中的方法进行扩展,再结合AOP思想,可以极大的方便我们的日常开发。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/117978.html