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格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...
随机推荐
- bzoj 1833
f[pos][top] 统计数字个数 g[pos][top][digit] 统计数位(对于f[pos][top]中的所有数) /************************************ ...
- Codeforces Round #346 (Div. 2) D. Bicycle Race 叉积
D. Bicycle Race 题目连接: http://www.codeforces.com/contest/659/problem/D Description Maria participates ...
- extjs用iframe的问题
项目中用extjs做前提系统的界面是左边用树做目录 右边用tabpanel做内容展示点击树节点的时候 在tabpanel添加新的tab JScript code var newTab = center ...
- sql 锁类型与锁机制 转
SQL Server锁类型(SQL)收藏1. HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁. 2. NOLOCK:不添加共享锁和排它锁,当这个 ...
- mogodb优化
http://snoopyxdy.blog.163.com/blog/static/6011744020157511536993/ http://www.csdn.net/article/2012-1 ...
- Installshield 2010 中集成. Net framework4 与 vc++ 2010运行安装包
1.prq的地址,通过以下地址,下载相应的prq文件 VC 2010 redist X86: http://saturn.installshield.com/is/prerequisites/micr ...
- B+树索引
结构上 B树中关键字集合分布在整棵树中,叶节点中不包含任何关键字信息,而B+树关键字集合分布在叶子结点中,非叶节点只是叶子结点中关键字的索引: B树中任何一个关键字只出现在一个结点中,而B+树中的关键 ...
- .NET:CLR via C# The CLR’s Execution Model
The CLR’s Execution Model The core features of the CLR memory management. assembly loading. security ...
- 3月题外:关于JS实现图片缩略图效果的一个小问题
由于3月可能要结束实习,所以应该不会有特别固定的主题,另外我会在月初陆续补上上个月的番外篇Projection和TMS,作为介绍性的内容对矢量切片部分进行补充,剩下时间不定期写一些杂烩. 最近两天在做 ...
- iOS: 如何获取ios设备的当前IP地址
有的时候,我们项目上线后,需要根据ip地址去统计不同地区的用户情况,此时IP地址的收取显得尤其重要,一般情况下,在用户登录时去获取用户的ip是准确的,当然实时追踪ip的变化而统计是更安全可靠的. ip ...