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

最新文章

  1. ORACLE 解锁、找回表和找回程序语句
  2. 实际项目中的一个angularjs 应用
  3. Autofac 的构造函数注入方式
  4. CentOS 安装Redis
  5. C函数之memcpy()函数用法
  6. rabbitMQ 远程访问
  7. CSS之perspective
  8. 怎么确定你的CPU是否支持64位虚拟化
  9. WARN Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect (org.apache.zookeeper.ClientCnxn)
  10. Angular中使用$watch监听
  11. python 集合去重
  12. Linux环境nginx的安装
  13. css制作tips提示框,气泡框,制作三角形
  14. 树莓派进阶之路 (019) - 树莓派通过filezilla,samba与PC文件共享(转)
  15. 怎样在 Ubuntu 上使用 ZFS 文件系统 | Linux 中国
  16. iOS开发多线程篇—GCD简介
  17. JavaScript入门学习(0)相关 软件工具
  18. SQL Server中使用自定义指定顺序排序
  19. PHP 循环删除无限分类子节点
  20. 本周PSP图

热门文章

  1. NOIp2018集训test-9-6(am)
  2. python从入门到大神---2、和Python编程相遇的日子
  3. AsyncAwait
  4. webpack3
  5. jq页面换肤效果
  6. vue多文件上传进度条 进度不更新问题
  7. 字符串KMP算法
  8. USACO2008 Cow Cars /// oj23323
  9. 数组模拟stack
  10. 在 Mac 上使用 `sed` 命令的几个坑