27、AOP-AOP功能测试

AOP : 【动态代理】指程序运行期间动态的将某段代码切入到制定方法位置进行运行的编程方式。


  1. 导入AOP模块:Spring AOP(spring-aspects)
  2. 定义一个业务逻辑类(Mathcalculator);在业务逻辑运行的时候将日志进行打印(方法之前、方法运行之后等)
  3. 定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator(div)运行到哪里,然后执行
  4. 方法通知:
    1. 前置通知(@Befor):LogStart
    2. 后置通知(@After):LogEnd
    3. 返回通知(@AfterReturning):LogReturn
    4. 异常通知(@AfterThrowing):LogException
    5. 环绕通知(@Aound):动态代理,手动推进目标方法运行(joinPoint.procced())
  5. 将切面类的目标犯法标注何时何地运行(通知注释);
  6. 将切面类和业务逻辑类(目标方法所在类 )加入到容器类;
  7. 配置类中@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功能测试的更多相关文章

  1. Spring 学习——Spring AOP——AOP概念篇

    AOP AOP的定义:AOP,Aspect Oriented Programming的缩写,意为面向切面编程,是通过预编译或运行期动态代理实现程序功能处理的统一维护的一种技术 实现方式 预编译 Asp ...

  2. Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)

    声明通知Advice 配置方式(以前置通知为例子) 方式一 <aop:config> <aop:aspect id="ikAspectAop" ref=" ...

  3. Spring 学习——Spring AOP——AOP配置篇Advice(无参数传递)

    声明通知Advice 配置方式(以前置通知为例子) 方式一 <aop:config> <aop:aspect id="ikAspectAop" ref=" ...

  4. Spring 学习——Spring AOP——AOP配置篇Aspect、Pointcut

    Schena——based AOP 声明 Spring所有的切面和通知器都必须放在一个<aop:config>标签内,可以同时配置多个<aop:config>元素. 每一个&l ...

  5. hibernate事务配置Aop aop:advisor模式

    <!-- 使用HibernateTransactionManager管理hibernate事务 --> <bean id="txManager" class=&q ...

  6. 重新学习Spring注解——AOP

    面向切面编程——思想:在一个地方定义通用功能,但是可以通过声明的方式定义这个功能要以何种方式在何处运用,而无须修改受影响的类. 切面:横切关注点可以被模块化为特殊的类. 优点: 1.每个关注点都集中在 ...

  7. Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知

    本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知     1. Spring AOP  前置通知 XML配置使用案例     2. Spring AOP   ...

  8. Spring第十篇—举例实现AOP

    简述AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入封 ...

  9. 160919、使用AOP与注解记录Java日志

    有些时候,我想要把每个运行过的方法接收到的参数.返回值和执行时间等信息记录(通过slf4j 和 log4j)下来.在AspectJ.jcabi-aspects和Java注解的帮助下我实现了这个想法. ...

随机推荐

  1. 把cgrep mgrep集成到bashrc

    https://android.googlesource.com/platform/build/+/android-4.4.3_r1/envsetup.sh 在~/.bashrc里面增加: #Andr ...

  2. Centos7.3安装nexus12.1

    nexus.12.1-01的安装             1.下载nexus             2.上传到服务器/root/             3.解压                 t ...

  3. Python--一些基础内容

    1. Content-Type是什么? Content-Type描述的只是发送端;发送端既可以是服务器也可以是客户端;Content-Type代表发送端发送的实体数据的数据类型.比如:Content- ...

  4. 5-9 c语言之【初识win32编程】

    ---恢复内容开始--- 今天学习了win32的相关知识,首先win32是指是指可以在32位或以上Windows系统中运行的程序,我学习的主要利用c/c++语言编写的win32程序, 首先在win32 ...

  5. ASM实例远程连接

    存在一个软件,远程连接ASM实例 tj2:/picclife/app/grid$ lsnrctl status Listening Endpoints Summary... (DESCRIPTION= ...

  6. Linux发布java jar包

    打包参考https://www.cnblogs.com/Rexcnblog/p/11357146.html 刚打包出来新鲜的jar 然后开始一顿猛如虎的操作了,把打包的jar和对用的sh文件拷贝到li ...

  7. 三、eureka服务端获取服务列表

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 eureka服务端维护了一个服务信息的列表,服务端节点之间相互复制服务信息.而作为eur ...

  8. 【转载】salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建

    salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建   VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的 ...

  9. S2-032

    前言 S2-032漏洞的影响范围是Struts 2.3.20 - Struts Struts 2.3.28,当开启了动态方法调用时可RCE.这次的漏洞分析以及后面的漏洞分析都是使用的Struts 2. ...

  10. 苹果发布app,上传ipa,不显示问题

    用Xcode或者leader上传ipa,提示是上传成功,但是在网页上不显示构建版本.如下图: 那么,你先点击“活动”,进去后,如果显示你的app正在审核,那么表示上传成功,等待:如果“活动”中不显示你 ...