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注解的帮助下我实现了这个想法. ...
随机推荐
- 路由器01---k2刷Pandora
1.固件 固件(Firmware)就是写入EPROM(可擦写可编程只读存储器)或EEPROM(电可擦可编程只读存储器)中的程序. 对于独立可操作的电子产品,固件一般指它的操作系统(“担任着一个数码产品 ...
- 关于db2中listagg函数开发中的体验
一.首先解释一下可能会查询的基础问题: 1.1db2 “with ur”是什么意思: 在DB2中,共有四种隔离级:RS,RR,CS,UR.以下对四种隔离级进行一些描述,同时附上个人做试验的结果.隔离级 ...
- Essential C++ Reading Notes
Chapter1 P6, 1.2 Why //#include<string> we still can use "string user_name"? -->c ...
- Linux (x86) Exploit 开发系列教程之一(典型的基于堆栈的缓冲区溢出)
(1)漏洞代码 //vuln.c #include <stdio.h> #include <string.h> int main(int argc, char* argv[]) ...
- Windows计划任务无法写Log的问题
参照:https://www.cnblogs.com/jonezzz/p/10364153.html 使用WIndows计划任务去执行Exe文件时无法写Log,而Exe双击执行就能写Log,这是由于计 ...
- redis哈希表数据类型键的设置
命令名称:hset 语法:hset key field value 功能: 1)将哈希表key中的域field的值设为value. 2)如果key不存在,一个新的哈希表被创建并进行hset操作. 3) ...
- (八)二进制文件在webservice中的处理(以byte[]字节数组方式)
一.介绍 二进制文件在webservice中的处理. A:通过byte[]字节数组的方式来传递.比较适合小文件,字节数组不能太大的情况.(本章所用) B:通过DataHander的方式来进行传递. 1 ...
- 通过SQL Server的扩展事件来跟踪SQL语句在运行时,时间都消耗到哪儿了?
原文:通过SQL Server的扩展事件来跟踪SQL语句在运行时,时间都消耗到哪儿了? 问题就是,一个很简单的语句,在不同的服务器上执行,所需要的时间相差很大,特别提到在性能差的服务器上反而快,在性能 ...
- ubuntu装openssh-client和openssh-server
1. 修改update源 进入/etc/apt/目录,首先用cp命令将sources.list备份成sources.list.bk,然后复制http://www.cnblogs.com/eastson ...
- Android高版本收不到静态注册的广播
Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-de ...