27、AOP-AOP功能测试
27、AOP-AOP功能测试
AOP : 【动态代理】指程序运行期间动态的将某段代码切入到制定方法位置进行运行的编程方式。
- 导入AOP模块:Spring AOP(spring-aspects)
- 定义一个业务逻辑类(Mathcalculator);在业务逻辑运行的时候将日志进行打印(方法之前、方法运行之后等)
- 定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator(div)运行到哪里,然后执行
- 方法通知:
- 前置通知(@Befor):LogStart
- 后置通知(@After):LogEnd
- 返回通知(@AfterReturning):LogReturn
- 异常通知(@AfterThrowing):LogException
- 环绕通知(@Aound):动态代理,手动推进目标方法运行(joinPoint.procced())
- 将切面类的目标犯法标注何时何地运行(通知注释);
- 将切面类和业务逻辑类(目标方法所在类 )加入到容器类;
- 配置类中@EnableAspectJAutoProxy 开启基于注解的AOP模式
27.1 Mathcalculator
package com.hw.springannotation.aop;
/**
* @Description
* @Author Administrator
* @Date 2018/12/1
*/
public class MathCalculator {
public int div(int i, int j) {
System.out.println("MathCalculator div 运行...");
return i / j;
}
}
27.2 LogAspects
package com.hw.springannotation.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import java.util.Arrays;
/**
* @Description
* @Author Administrator
* @Date 2018/12/1
*/
@Aspect // 告诉SPRING 当前类是一个切面类
public class LogAspects {
@Pointcut("execution(public int com.hw.springannotation.aop.MathCalculator.*(..))")
public void pointCut() {
}
@Before("pointCut()")
public void logStart(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName(); //方法名
Object[] args = joinPoint.getArgs(); //参数列表
System.out.println(methodName + "运行,参数列表是:{" + Arrays.asList(args) + "}");
}
@After("pointCut()")
public void logEnd(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature().getName() + "除法结束。。。");
}
@AfterReturning(value = "pointCut()", returning = "result")
public void logReturn(JoinPoint joinPoint, Object result) {
System.out.println(joinPoint.getSignature().getName() + "除法正常返回,运行结果是:" + result);
}
@AfterThrowing(value = "pointCut()", throwing = "exception")
public void logException(JoinPoint joinPoint, Exception exception) {
System.out.println(joinPoint.getSignature().getName() + "除法运行异常,异常信息:" + exception.getMessage());
}
}
27.3 配置类 MainConfigOfAop
package com.hw.springannotation.config;
import com.hw.springannotation.aop.LogAspects;
import com.hw.springannotation.aop.MathCalculator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* @Description
* @Author Administrator
* @Date 2018/12/1
*/
@Configuration
@EnableAspectJAutoProxy
public class MainConfigOfAop {
// 业务逻辑类加入到容器中
@Bean
public MathCalculator mathCalculator() {
return new MathCalculator();
}
// 切面类 也加入到容器中
@Bean
public LogAspects logAspects() {
return new LogAspects();
}
}
27.4 测试用例
@Test
public void test() {
// 不要自己创建对象
// MathCalculator calculator = new MathCalculator();
// calculator.div(1, 1);
// 从容器中获取
MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
mathCalculator.div(1, 0);
applicationContext.close();
}
运行:

注意:
JoinPoint joinPoint参数一定要写在 参数位置的第一位
27、AOP-AOP功能测试的更多相关文章
- Spring 学习——Spring AOP——AOP概念篇
AOP AOP的定义:AOP,Aspect Oriented Programming的缩写,意为面向切面编程,是通过预编译或运行期动态代理实现程序功能处理的统一维护的一种技术 实现方式 预编译 Asp ...
- Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)
声明通知Advice 配置方式(以前置通知为例子) 方式一 <aop:config> <aop:aspect id="ikAspectAop" ref=" ...
- Spring 学习——Spring AOP——AOP配置篇Advice(无参数传递)
声明通知Advice 配置方式(以前置通知为例子) 方式一 <aop:config> <aop:aspect id="ikAspectAop" ref=" ...
- Spring 学习——Spring AOP——AOP配置篇Aspect、Pointcut
Schena——based AOP 声明 Spring所有的切面和通知器都必须放在一个<aop:config>标签内,可以同时配置多个<aop:config>元素. 每一个&l ...
- hibernate事务配置Aop aop:advisor模式
<!-- 使用HibernateTransactionManager管理hibernate事务 --> <bean id="txManager" class=&q ...
- 重新学习Spring注解——AOP
面向切面编程——思想:在一个地方定义通用功能,但是可以通过声明的方式定义这个功能要以何种方式在何处运用,而无须修改受影响的类. 切面:横切关注点可以被模块化为特殊的类. 优点: 1.每个关注点都集中在 ...
- Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知
本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP ...
- Spring第十篇—举例实现AOP
简述AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入封 ...
- 160919、使用AOP与注解记录Java日志
有些时候,我想要把每个运行过的方法接收到的参数.返回值和执行时间等信息记录(通过slf4j 和 log4j)下来.在AspectJ.jcabi-aspects和Java注解的帮助下我实现了这个想法. ...
随机推荐
- 前端开发 — HTML
HTML HTML 超文本标记语言 HTML特征: 对换行和空格不敏感 空白折叠 1.1 HTML标签 标签也称为标记. 标签的种类: 1.双闭合标签 2.单闭合标签 1.1.1 head标签 met ...
- 针对Quizii的基本抓包实验(Fiddler)
本实验仅限于了解POST和GET几种常规数据获取方案的学习.简单的来说,POST就是向服务器发送数据,GET会向服务器请求数据.可以参考https://www.zhihu.com/question/2 ...
- Django中常用的那些模块路径
Django中常用的那些模块路径 from django.shortcuts import HttpResponse, render, redirect from django import temp ...
- 将物理机系统转为虚拟机系统 p2v
ref : https://blog.csdn.net/gsls200808/article/details/77932713 背景: 在公司有台机子主要负责某产品的升级与维护,出于各种原因,该产品需 ...
- sping+redis实现消息队列的乱码问题
使用spring支持redis实现消息队列,参考官方样例:https://spring.io/guides/gs/messaging-redis/ 实现后在运行过程中发现消费者在接收消息时会出现乱码的 ...
- (十二)Activitivi5之流程控制网关:排他
一.概念 所谓排他网关 顾名思义 执行到该网关,根据条件只能走一条执行线 二.案例 当流程走到“学生请假”任务节点的时候, /** * 完成任务 */ @Test public void test_c ...
- vue 集成 vis-network 实现网络拓扑图
vis.js 网站 https://visjs.org/ vs code 下安装命令 npm install vis-network 在vue 下引入 vis-network组件 const v ...
- .NET Core 常用第三方包
.NET Core 常用第三方包 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10845370.html 写在前面 最近在学习.NET Core 中经常用 ...
- java小工具:通过URL连接爬取资源(图片)
java语言编写一个简单爬取网站图片工具,实现简单: 通过 java.net.HttpURLConnection 获取一个URL连接 HttpURLConnection 连接成功返回一个java.io ...
- Uploadify 之使用
uploadify 3.2.1是 jQuery提供的一个上传插件,其参数详解见 http://www.cnblogs.com/yangy608/p/3915349.html 这里列举一个实际应用的例子 ...