使用XML的方式编写:@Aspect运用
例子、
接口
public interface Calculator {
// 加
public int add(int i, int j);
// 减
public int sub(int i, int j);
// 乘
public int mul(int i, int j);
//除
public int div(int i, int j);
}
实现
public class MyMathCalculator implements Calculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
切面、
public class LogUtils {
public static void logStart(JoinPoint joinPoint){
//获取到目标方法运行是使用的参数
Object[] args = joinPoint.getArgs();
//获取到方法签名
Signature signature = joinPoint.getSignature();
String name = signature.getName();
System.out.println("【"+name+"】方法开始执行,用的参数列表【"+ Arrays.asList(args)+"】");
}
public static void logReturn(JoinPoint joinPoint,Object result){
System.out.println("【"+joinPoint.getSignature().getName()+"】方法正常执行完成,计算结果是:"+result);
}
public static void logException(JoinPoint joinPoint,Exception exception){
System.out.println("【"+joinPoint.getSignature().getName()+"】方法执行出现异常了,异常信息是【"+exception+"】:;这个异常已经通知测试小组进行排查");
}
private int logEnd(JoinPoint joinPoint){
System.out.println("【"+joinPoint.getSignature().getName()+"】方法最终结束了");
return 0;
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- 基于注解的AOP步骤;
1、将目标类和切面类都加入到ioc容器中。@Component
2、告诉Spring哪个是切面类。@Aspect
3、在切面类中使用五个通知注解来配置切面中的这些通知方法都何时何地运行
4、开启基于注解的AOP功能
-->
<!-- 开启基于注解的AOP功能;aop名称空间-->
<!-- 基于配置的AOP-->
<bean id="myMathCalculator" class="com.apcstudy.aop.impl.MyMathCalculator" />
<bean id="logUtils" class="com.apcstudy.aop.utils.LogUtils" />
<!-- 需要AOP名称空间 -->
<aop:config>
<aop:pointcut id="mypoint" expression="execution(* com.apcstudy.aop.impl.MyMathCalculator.*(..))"/>
<!--告诉Spring哪个是切面类。@Aspect-->
<aop:aspect ref="logUtils">
<aop:before method="logStart" pointcut-ref="mypoint" />
<aop:after-returning method="logReturn" pointcut-ref="mypoint" returning="result" />
<aop:after-throwing method="logException" pointcut-ref="mypoint" throwing="exception" />
<aop:after method="logEnd" pointcut-ref="mypoint" />
</aop:aspect>
</aop:config>
<!--注解:快速方便
配置:功能完善;重要的用配置,不重要的用注解;
-->
</beans>
使用XML的方式编写:@Aspect运用的更多相关文章
- 21_Android中常见对话框,光传感器,通过重力感应器编写出指南针应用,帧动画,通过Jav代码的方式编写补间动画,通过XML的方式编写补间动画
1 关于常见的对话框,主要有: 常见的对话框,单选对话框,多选对话框,进度条对话框(转圈类型的),带进度条的对话框. 案例结构: 完成如下结构的案例,将所有的案例都测试一下: 2 编写MainA ...
- 转-springAOP基于XML配置文件方式
springAOP基于XML配置文件方式 时间 2014-03-28 20:11:12 CSDN博客 原文 http://blog.csdn.net/yantingmei/article/deta ...
- Android中的三种XML解析方式
在Android中提供了三种解析XML的方式:SAX(Simple API XML),DOM(Document Objrect Model),以及Android推荐的Pull解析方式.下面就对三种解析 ...
- struts_20_对Action中所有方法、某一个方法进行输入校验(基于XML配置方式实现输入校验)
第01步:导包 第02步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app ...
- struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)
课时22 基于XML配置方式实现对action的所有方法进行校验 使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...
- 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的全部方法进行输入校验
在struts2中,我们能够实现对action的全部方法进行校验或者对action的指定方法进行校验. 对于输入校验struts2提供了两种实现方法: 1. 採用手工编写代码实现. 2. 基于XML配 ...
- Struts2第十篇【数据校验、代码方式、XML配置方式、错误信息返回样式】
回顾以前的数据校验 使用一个FormBean对象来封装着web端来过来的数据 维护一个Map集合保存着错误信息-对各个字段进行逻辑判断 //表单提交过来的数据全都是String类型的,birthday ...
- 6.AOP配置与应用(xml的方式)
xml 配置 AOP 1.将 拦截其器对象 初始化到容器中 2.<aop:config> <aop:aspect.... <aop:pointcut <aop:befor ...
- hibernate 联合主键生成机制(组合主键XML配置方式)
hibernate 联合主键生成机制(组合主键XML配置方式) 如果数据库中用多个字段而不仅仅是一个字段作为主键,也就是联合主键,这个时候就可以使用hibernate提供的联合主键生成策略. 具体 ...
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
随机推荐
- MapStruct入门使用
MapStruct入门使用案例 以下是常用的使用举例,按照需求改动即可 @Data public class UserDO{ private int age; private String name; ...
- 数据存储“取经路”,HBlock轻松“渡”!
近日,天翼云联合权威科技媒体InfoQ举办了以"新存储,更轻量"为主题的线上技术分享会.天翼云存储产品线总监武志民讲解了HBlock的创新设计和技术. 高性能·高可用·高可靠 自研 ...
- 搞懂TVS管,有这篇文章就够了
摘要:本文主要介绍TVS的工作原理.关键参数和选型. TVS(Transient Voltage Suppressors,瞬态电压抑制器)又称雪崩击穿二极管,是一种高效电路保护器件,主要是保护电路不受 ...
- Linux mint的hadoop安装方法
参考网址http://www.powerxing.com/install-hadoop/ 1.创建hadoop账户 这条命令创建了可以登陆的 hadoop 用户,并使用 /bin/bash 作为 sh ...
- Gradle的安装及换源详细教程
Gradle是一个基于JVM的构建工具,用于自动化构建.测试和部署项目. 1. 安装Gradle a. 首先,确保你已经安装了Java Development Kit (JDK),并且已经配置了JAV ...
- Spring Boot创建完项目运行Cannot determine embedded database driver class for database type NONE
spring boot 创建项目引用mybatis后,直接运行会提示: Cannot determine embedded database driver class for database typ ...
- 读论文-电子商务产品推荐的序列推荐系统综述与分类(A Survey and Taxonomy of Sequential Recommender Systems for E-commerce Product Recommendation)
前言 今天读的这篇文章是于2023年发表在"SN Computer Science"上的一篇论文,这篇文章主要对序列推荐系统进行了全面的调查和分类,特别是在电子商务领域的应用.文章 ...
- JVM运行参数
一.三种参数类型 1.标准参数:比较稳定,以后版本会保留 -help -version 2.-X参数(非标准参数) -Xint -Xcomp 3.-XX参数(非标准参数,使用率较高) -XX:newS ...
- 使用PySide6/PyQt6实现Python跨平台表格数据分页打印预览处理
我曾经在前面使用WxPython开发跨平台应用程序的时候,写了一篇<WxPython跨平台开发框架之列表数据的通用打印处理>,介绍在WxPython下实现表格数据分页打印处理的过程,在Wi ...
- nginx 根据 URL 参数引入不同的文件
同步发布:https://blog.jijian.link/2020-06-30/nginx-import-file/ 编程世界中各种奇奇怪怪的需求都有,本次遇到一个需求:根据URL参数判断,包含 x ...