当前位置: 首页 > news >正文

Spring AOP - 配置文件方式实现

目录

AOP基础概念

示例1:模拟在com.text包及子包项下所有类名称以ServiceImpl结尾的类的所有方法执行前、执行后、执行正常后返回值、执行过程中出异常的情况

示例2:统计com.text包及子包项下所有类名称以DaoImpl结尾的类的所有方法执行时长情况


AOP基础概念

AOP:Aspect Oriented Programming 面向切面编程

使用场景:将一些通用的功能封装成切面类,切面类作用在目标类方法的前后,并通过自动插拔实现目标类方法的前后逻辑,示意图如下:

上述的示意图显示,Aspect切面类横向贯穿了3个目标类方法的执行逻辑之前,由此可以看出,AOP实现需要如下组件:

  1. 切面类(Aspect类)
  2. 切点(Pointcut),即上图的各个目标方法,通过切点表达式(execution)实现
  3. 连接点(JoinPoint) ,切面和切点之间的连接信息,可以理解为横切面和切点的交汇处
  4. 通知(Advice) ,在目标类方法的之前、之后还是环绕执行切面逻辑

Spring AOP实现需要引入aspectjweaver依赖

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.6</version>
</dependency>

示例1:模拟在com.text包及子包项下所有类名称以ServiceImpl结尾的类的所有方法执行前、执行后、执行正常后返回值、执行过程中出异常的情况

1、配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--<context:component-scan base-package="com.text"/>--><bean id="studentService" class="com.text.service.impl.StudentServiceImpl"/><!-- 切面类交给IOC容器实例 --><bean id="methodAspect" class="com.text.aspect.MethodAspect"/><!-- aop配置--><aop:config><!-- 切点配置,expression表示com.text所有包及子包项下所有服务层(以ServiceImpl结束的类)的所有方法--><aop:pointcut id="pointcut" expression="execution(public * com.text..*ServiceImpl.*(..))"/><!-- 切面配置--><aop:aspect ref="methodAspect"><!-- 切点方法执行之前执行--><aop:before method="methodInvokeBefore" pointcut-ref="pointcut"/><!-- 切点方法执行之后执行--><aop:after method="methodInvokeAfter" pointcut-ref="pointcut"/><!-- 切点方法执行之后带返回值--><aop:after-returning method="methodInvokeAfterReturn" returning="ret" pointcut-ref="pointcut"/><!-- 切点方法执行时出现异常--><aop:after-throwing method="methodInvokeException" throwing="thr" pointcut-ref="pointcut"/></aop:aspect></aop:config></beans>

2、服务类及方法(pointcut)

package com.text.service.impl;import com.text.entity.Course;
import com.text.entity.Student;
import com.text.service.StudentService;public class StudentServiceImpl implements StudentService {@Overridepublic void save(Student student) {System.out.println(student + "正在被保存...");}@Overridepublic void deleteById(String id) {System.out.println("学生id=" + id + "的记录已被删除...");}@Overridepublic void updateById(String id) throws Exception{System.out.println("学生id=" + id + "的记录正在被修改...");throw new Exception("修改学生信息出异常");}@Overridepublic Student searchById(String id) {System.out.println("已查询到学生id=" + id + "的记录...");return new Student("张三",20,new Course("计算机"));}
}

3、切面类(aspect) 

package com.text.aspect;import org.aspectj.lang.JoinPoint;/*** 定义方法切面类*/
public class MethodAspect {public void methodInvokeBefore(JoinPoint joinPoint) {String targetClassName = joinPoint.getTarget().getClass().getName();//获取切面执行的目标类String methodName = joinPoint.getSignature().getName();System.out.println(targetClassName + "." + methodName + "方法执行之前的处理逻辑...");}public void methodInvokeAfter(JoinPoint joinPoint) {String targetClassName = joinPoint.getTarget().getClass().getName();//获取切面执行的目标类String methodName = joinPoint.getSignature().getName();System.out.println(targetClassName + "." + methodName + "方法执行之后的处理逻辑...");}public void methodInvokeAfterReturn(JoinPoint joinPoint,Object ret) {String targetClassName = joinPoint.getTarget().getClass().getName();//获取切面执行的目标类String methodName = joinPoint.getSignature().getName();System.out.println(targetClassName + "." + methodName + "方法执行之后返回的结果为:" + ret);}public void methodInvokeException(JoinPoint joinPoint,Throwable thr) throws Throwable {String targetClassName = joinPoint.getTarget().getClass().getName();//获取切面执行的目标类String methodName = joinPoint.getSignature().getName();System.out.println(targetClassName + "." + methodName + "方法执行中的异常信息为:" + thr.getMessage());throw thr;}
}

4、测试类 Application

package com.text;
import com.text.entity.Course;
import com.text.entity.Student;
import com.text.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Application {public static void main(String[] args) throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");StudentService studentService = context.getBean("studentService", StudentService.class);studentService.save(new Student("张三",20,new Course("计算机")));System.out.println("======save end========");studentService.deleteById("1");System.out.println("======delete end ========");Student student = studentService.searchById("1");System.out.println("======search end ========");studentService.updateById("1");System.out.println("======update end ========");}
}

5、输出结果:

示例2:统计com.text包及子包项下所有类名称以DaoImpl结尾的类的所有方法执行时长情况

此需求如果按照之前的advice至少需要写2个,一个是aop:before,一个是aop:after,方法的总执行时间需要after的当前时间减去before的当前时间,这2个时间如何联通也存在困难,AOP 提供一种自定义的通知执行时机-Around Advice(环绕通知)可以轻松解决此需求

1、配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--<context:component-scan base-package="com.text"/>--><bean id="studentDao" class="com.text.dao.impl.StudentDaoImpl"/><!-- 切面类交给IOC容器实例 --><bean id="methodAspect" class="com.text.aspect.MethodAspect"/><!-- aop配置--><aop:config><!-- 切点配置,expression表示com.text所有包及子包项下所有服务层(以DaoImpl结束的类)的所有方法--><aop:pointcut id="pointcut" expression="execution(public * com.text..*DaoImpl.*(..))"/><!-- 切面配置--><aop:aspect ref="methodAspect"><!-- 自定义的环绕通知--><aop:around method="countMethodInvokeTime" pointcut-ref="pointcut"/></aop:aspect></aop:config></beans>

2、服务类及方法(pointcut)

package com.text.dao.impl;
import com.text.dao.StudentDao;public class StudentDaoImpl implements StudentDao {@Overridepublic void getById(String id) throws Exception {Thread.sleep(1000);System.out.println("查询学生id=" + id + "的信息");}
}

3、切面类(aspect) 

package com.text.aspect;import org.aspectj.lang.ProceedingJoinPoint;import java.util.Date;/*** 定义方法切面类*/
public class MethodAspect {public void countMethodInvokeTime(ProceedingJoinPoint proceedingJoinPoint) {System.out.println("目标方法执行之前记录初始时间...");Date startTime = new Date();try {proceedingJoinPoint.proceed();//执行目标方法 即:StudentDaoImpl.getById方法System.out.println("目标方法执行之后记录结束时间...");String methodName = proceedingJoinPoint.getTarget().getClass().getName() + "." +proceedingJoinPoint.getSignature().getName();Date endTime = new Date();System.out.println(methodName + "方法执行总时长为:" + (endTime.getTime() - startTime.getTime()) + "毫秒");} catch (Throwable throwable) {throwable.printStackTrace();}}
}

4、测试类 Application

package com.text;import com.text.dao.StudentDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Application {public static void main(String[] args) throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");StudentDao studentDao = context.getBean("studentDao", StudentDao.class);studentDao.getById("1");System.out.println("======getById end========");}
}

5、输出结果:


http://www.mrgr.cn/news/33601.html

相关文章:

  • Linux进阶命令-rsync daemon
  • 【通讯协议】S32K142芯片——LIN通信的学习和配置
  • 解决docker指令卡住的场景之一
  • KTH5702系列 低功耗、高精度 2D 霍尔旋转位置传感器 车规AEC-Q100
  • 01 基础request
  • linux之进程信号
  • 【网络安全】依赖混淆漏洞实现RCE
  • java Nio的应用
  • OpenCV特征检测(9)检测图像中直线的函数HoughLines()的使用
  • 命名管道详解
  • 用最容易理解的方法,实现LRU、LFU算法
  • C#如何把写好的类编译成dll文件
  • ArcGIS核密度分析(栅格处理范围与掩膜分析)
  • NLP:命名实体识别及案例(Bert微调)
  • Redis:常用命令总结
  • 【机器学习】——线性回归(自我监督学习)
  • 基于ECC簇内分组密钥管理算法的无线传感器网络matlab性能仿真
  • Python画笔案例-058 绘制单击画酷炫彩盘
  • 3.递归求值
  • AI 智能名片链动 2+1 模式商城小程序中的体验策略