Spring 基于xml配置方式的AOP(8)
1、ArithmeticCalculator.java

1 package com.proc;
2
3 public interface ArithmeticCalculator {
4 int add(int i, int j);
5 int sub(int i, int j);
6
7 int mul(int i, int j);
8 int div(int i, int j);
9 }

2、ArithmeticCalculatorImpl.java 实现接口ArithmeticCalculator

1 package com.proc;
2
3 public class ArithmeticCalculatorImpl implements ArithmeticCalculator{
4 public int add(int i, int j) {
5 int result = i + j;
6 return result;
7 }
8
9 public int sub(int i, int j) {
10 int result = i - j;
11 return result;
12 }
13
14 public int mul(int i, int j) {
15 int result = i * j;
16 return result;
17 }
18
19 public int div(int i, int j) {
20 int result = i / j;
21 return result;
22 }
23 }

3、LoggingAspect.java 日志切面

1 package com.proc;
2
3 import org.aspectj.lang.JoinPoint;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.After;
6 import org.aspectj.lang.annotation.AfterReturning;
7 import org.aspectj.lang.annotation.AfterThrowing;
8
9
10 public class LoggingAspect {
11
12 public void beforeMethod(JoinPoint point){
13 System.out.println("正在执行方法: "+point.getSignature().getName());
14 }
15
16
17 public void afterMethod(JoinPoint point){
18 System.out.println("方法执行结束: "+point.getSignature().getName());
19 }
20
21
22 public void afterReturningMethod(JoinPoint point,Object retVal){
23 System.out.println("方法: "+point.getSignature().getName()+"执行结果为:"+retVal);
24 }
25
26
27 public void afterThrowingMethod(JoinPoint point,Exception ex){
28 System.out.println("执行方法: "+point.getSignature().getName()+"出现了异常:"+ex.getMessage());
29 }
30
31 public Object aroundMethod(ProceedingJoinPoint point){
32
33 System.out.println("环绕通知: "+point.getSignature().getName());
34 Object result=null;
35 //这里相当于前置通知
36 try {
37 //执行方法
38 result= point.proceed();
39 //这里相当于结果通知
40 } catch (Throwable e) {
41 //这里相当于异常通知
42 e.printStackTrace();
43
44 }
45 //这里相当于后置通知
46 System.out.println("环绕通知: "+point.getSignature().getName());
47 return result;
48 }
49 }

其实这也就是一个普通类,里面定义了写方法
4、ValidateAspect.java 验证切面

1 package com.proc;
2
3 import org.aspectj.lang.JoinPoint;
4
5 public class ValidateAspect {
6 public void beforeMethod(JoinPoint point){
7 System.out.println("验证前置通知: "+point.getSignature().getName());
8 }
9 }

5、applicationContext.xml配置

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
7
8 <bean id="arithmeticCalculator" class="com.proc.ArithmeticCalculatorImpl"/>
9 <bean id="loggingAspect" class="com.proc.LoggingAspect" />
10 <bean id="validateAspect" class="com.proc.ValidateAspect"/>
11
12 <aop:config>
13 <!-- 配置切面表达式 -->
14 <aop:pointcut expression="execution(* *..*(..))" id="pointcut"/>
15
16 <!-- 配置切面及通知 -->
17 <aop:aspect ref="loggingAspect" order="1">
18 <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
19 <aop:after method="afterMethod" pointcut-ref="pointcut"/>
20 <aop:after-returning method="afterReturningMethod" pointcut-ref="pointcut" returning="retVal"/>
21 <aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex"/>
22 <aop:around method="aroundMethod" pointcut-ref="pointcut" />
23 </aop:aspect>
24
25 <aop:aspect ref="validateAspect" order="2">
26 <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
27 </aop:aspect>
28 </aop:config>
29 </beans>

测试代码:

1 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
2 ArithmeticCalculator calc=(ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
3 System.out.println(calc.add(3, 5));
4 System.out.println(calc.sub(3, 5));
5 System.out.println(calc.mul(6, 2));
6 System.out.println(calc.div(6, 2));

输出结果:
正在执行方法: add
环绕通知: add
验证前置通知: add
环绕通知: add
方法: add执行结果为:8
方法执行结束: add
8
正在执行方法: sub
环绕通知: sub
验证前置通知: sub
环绕通知: sub
方法: sub执行结果为:-2
方法执行结束: sub
-2
正在执行方法: mul
环绕通知: mul
验证前置通知: mul
环绕通知: mul
方法: mul执行结果为:12
方法执行结束: mul
12
正在执行方法: div
环绕通知: div
验证前置通知: div
环绕通知: div
方法: div执行结果为:3
方法执行结束: div
Spring 基于xml配置方式的AOP(8)的更多相关文章
- Spring 基于xml配置方式的AOP
我们具体用代码来说明: 1.ArithmeticCalculator.java package com.proc; public interface ArithmeticCalculator { in ...
- Spring 基于xml配置方式的事务
参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...
- Spring 基于xml配置方式的事务(14)
参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...
- 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基于XML配置方式实现对action的所有方法进行输入校验
出处:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.html http://www.blogjava.net/focusJ/arc ...
- ssm整合(基于xml配置方式)
本文是基于xml配置的方式来整合SpringMVC.Spring和Mybatis(基于注解的方式会再写一篇文章),步骤如下: (1)首先自然是依赖包的配置文件 pom.xml <project ...
- Spring 基于XML配置
基于XML的配置 对于基于XML的配置,Spring 1.0的配置文件采用DTD格式,Spring2.0以后采用Schema格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...
随机推荐
- awk 一 文本处理工具
简介 awk 是逐行扫描文件(从第1行到最后一行),寻找含有目标文本的行: 如果匹配成功,则会在该行上执行用户想要的操作. 反之,则不对行做任何处理. awk 命令的基本格式为: awk [选项] ' ...
- iOS开发inputView和inputAccessoryView
1.简介 起初看到这两个属性是在UIResponder中,只是可读的: @property (nullable, nonatomic, readonly, strong) __kindof UIVie ...
- Mysql命令增加、修改、删除表字段
alter add 命令用来增加表的字段: alter add命令格式:alter table 表名 add字段 类型 其他:如下所示: ) comment '单位' alter drop 命令删除表 ...
- 文本数据和mysql 里面的数据比较
实现读取TXT文件中的内容然后存到内存,然后将内存中的数据和mysql 数据库里面某张表数据的字段做一个比较,如果比较内存中的数据在mysql 里存在则不做处理,如果不存在则将该数据插入mysql数据 ...
- 阿里数据库大牛的 MySQL 学习指南!
做后端的同学,总是绕不开MySQL. 毫无疑问,MySQL 是当下最流行的开源数据库.凭借强大的性能和易于使用性,它已被Google.Facebook.YouTube.百度.网易和新浪等大型互联网公司 ...
- java-day14
多线程程序访问共享数据会产生安全问题 解决线程安全问题 同步代码块 synchronized(锁对象){ 可能出现线程问题的代码 } 同步方法 修饰符 synchronized 返回值类型 方法名() ...
- scrapy的使用-LinkExtractor
背景: 在爬取网站信息是需要获取特定标签下的某些内容,就需要获取这些标签下的链接,如果获取每一个,在通过这个获取它下面的信息,这样效率会很低,时间复杂度O(n^2),但如果先获取链接,再获取内容,则时 ...
- POJ 3237 /// 树链剖分 线段树区间修改(*-1)
题目大意: 给定树的N个结点 编号为1到N 给定N-1条边的边权. 三种操作: CHANGE k w:将第 k 条边的权值改成 w. NEGATE x y:将x到y的路径上所有边的权值乘 -1. QU ...
- 【洛谷】P1962
题目链接:https://www.luogu.org/problemnew/show/P1962 题意:求fib数列的第n项,很大.mod 1e9+7. 题解:BM直接推. 代码: #include ...
- 【2018ACM/ICPC网络赛】沈阳赛区
这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...