Spring 基于xml配置方式的AOP
我们具体用代码来说明:
1、ArithmeticCalculator.java
package com.proc;
public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
2、ArithmeticCalculatorImpl.java 实现接口ArithmeticCalculator
package com.proc;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator{
public int add(int i, int j) {
int result = i + j;
return result;
}
public int sub(int i, int j) {
int result = i - j;
return result;
}
public int mul(int i, int j) {
int result = i * j;
return result;
}
public int div(int i, int j) {
int result = i / j;
return result;
}
}
3、LoggingAspect.java 日志切面
package com.proc; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing; public class LoggingAspect { public void beforeMethod(JoinPoint point){
System.out.println("正在执行方法: "+point.getSignature().getName());
} public void afterMethod(JoinPoint point){
System.out.println("方法执行结束: "+point.getSignature().getName());
} public void afterReturningMethod(JoinPoint point,Object retVal){
System.out.println("方法: "+point.getSignature().getName()+"执行结果为:"+retVal);
} public void afterThrowingMethod(JoinPoint point,Exception ex){
System.out.println("执行方法: "+point.getSignature().getName()+"出现了异常:"+ex.getMessage());
} public Object aroundMethod(ProceedingJoinPoint point){ System.out.println("环绕通知: "+point.getSignature().getName());
Object result=null;
//这里相当于前置通知
try {
//执行方法
result= point.proceed();
//这里相当于结果通知
} catch (Throwable e) {
//这里相当于异常通知
e.printStackTrace(); }
//这里相当于后置通知
System.out.println("环绕通知: "+point.getSignature().getName());
return result;
}
}
其实这也就是一个普通类,里面定义了写方法
4、ValidateAspect.java 验证切面
package com.proc;
import org.aspectj.lang.JoinPoint;
public class ValidateAspect {
public void beforeMethod(JoinPoint point){
System.out.println("验证前置通知: "+point.getSignature().getName());
}
}
5、applicationContext.xml配置
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="arithmeticCalculator" class="com.proc.ArithmeticCalculatorImpl"/>
<bean id="loggingAspect" class="com.proc.LoggingAspect" />
<bean id="validateAspect" class="com.proc.ValidateAspect"/> <aop:config>
<!-- 配置切面表达式 -->
<aop:pointcut expression="execution(* *..*(..))" id="pointcut"/> <!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="1">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturningMethod" pointcut-ref="pointcut" returning="retVal"/>
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex"/>
<aop:around method="aroundMethod" pointcut-ref="pointcut" />
</aop:aspect> <aop:aspect ref="validateAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
测试代码:
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
ArithmeticCalculator calc=(ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
System.out.println(calc.add(3, 5));
System.out.println(calc.sub(3, 5));
System.out.println(calc.mul(6, 2));
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
3
Spring 基于xml配置方式的AOP的更多相关文章
- Spring 基于xml配置方式的AOP(8)
1.ArithmeticCalculator.java 1 package com.proc; 2 3 public interface ArithmeticCalculator { 4 int ad ...
- 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格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...
随机推荐
- 【点分治】【路径小于等于k的条数】【路径恰好等于k是否存在】
POJ1741:Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29574 Accepted: 9915 Des ...
- bzoj 4874: 筐子放球
4874: 筐子放球 Time Limit: 10 Sec Memory Limit: 256 MB Description 小N最近在研究NP完全问题,小O看小N研究得热火朝天,便给他出了一道这样 ...
- VK Cup 2015 - Round 1 E. Rooks and Rectangles 线段树 定点修改,区间最小值
E. Rooks and Rectangles Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemse ...
- Unity快捷键总结
Shift+Alt+A 物体快速激活 Ctrl+P 开始 Ctrl+Shift+P 暂停 Ctrl+B 编译并运行 Z Pivot/Center切换 X Local/Global切换
- PAT甲级1017. Queueing at Bank
PAT甲级1017. Queueing at Bank 题意: 假设一家银行有K台开放服务.窗前有一条黄线,将等候区分为两部分.所有的客户都必须在黄线后面排队,直到他/她轮到服务,并有一个可用的窗口. ...
- Ubuntu14.04环境下配置TFTP服务器
<<<<<<<<<<<<<<<<<<<<<<<<< ...
- The Struts dispatcher cannot be found. This is usually caused by using Struts ta
HTTP Status 500 - type Exception report message description The server encountered an internal error ...
- Gulp插件autoprefixer的使用
1.创建:gulpfile.js //引入插件 var gulp = require('gulp'); var autoprefixer = require('gulp-autoprefixer'); ...
- 1503162139-ny-分数拆分
分数拆分 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描写叙述 如今输入一个正整数k,找到全部的正整数x>=y,使得1/k=1/x+1/y. 输入 第一行输入一个整数 ...
- WPF性能调试系列 – Ants Performance Profiler
WPF性能调试系列文章: WPF页面渲染优化:Application Timeline WPF页面业务加载优化:Ants Performance Profiler WPF内存优化:Ants Memor ...