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格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...
随机推荐
- Java基础学习——多线程之创建任务
这次来盘点一下Java中用线程执行任务的写法. 1.扩展Thread 最基本的实现方法是在创建一个继承Thread的新类,在其中覆盖run()方法执行任务. public class MyThread ...
- 解决XP系统访问Win10打印机被拒绝的问题
打印机是办公室人员经常会用到的设备,为了方便多人使用都会将打印机设置共享,可是会有许多xp系统用户需要访问win10系统上的打印机,这时候却发现拒绝访问无法连接,该如何解决呢? 其实这是win10做的 ...
- 24.最优布线问题(kruskal算法)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用 ...
- python开发_gzip_压缩|解压缩gz文件_完整版_博主推荐
''' gzip -- 支持gzip文件 源文件:Lib/gzip.py 这个模块提供了一些简单的接口来对文件进行压缩和解压缩,类似于GNU项目的gzip和gunzip. 数据的压缩源于zlib模块的 ...
- MYSQL学习笔记 (三)JOIN用法
数据库的操作分开增删改查,其中查询操作基本占系统的90%,大家所说的优化SQL语句基本是优化查询语句.接下来将学习JOIN的用法,JOIN包括:INNER JOIN(内连接).LEFT JOIN(左外 ...
- SilverLight学习笔记--使用WebClient实现通讯(一)(上传和下载字符串数据)
一.什么是WebClient类 1.基本知识 WebClient类是Mircsoft在.NET框架下提供的向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法.通过这个类 ...
- Practice safe dc/dc converter
Short-circuit protection is an obvious requirement for a power supply, especially when its load conn ...
- Marvell w8782 sdio wifi AP模式设置
http://blog.csdn.net/junllee/article/details/8895908 w8782工作在station模式一切正常,于是想试试AP模式(master mode): A ...
- OOW 2015 MYSQL
https://events.rainfocus.com/oow15/catalog/oracle.jsp?search.event=openworldEvent&search.mysql=d ...
- sourceinsight使用技巧
转:http://blog.csdn.net/flyyanqu/article/details/2222799 目录(?)[-] 配置成简单好用的cjava代码编辑器 缩进与tab 向项目中添加文件时 ...