Spring(二十):Spring AOP(四):基于配置文件的方式来配置 AOP
基于配置文件的方式来配置 AOP
前边三个章节《Spring(十七):Spring AOP(一):简介》、《Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)》、《Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式》讲解AOP时,都采用的是注解方式,如何使用配置文件的方式配置AOP呢?那么,本章节就会使用前边章节的例子,基于文件配置的方式实现AOP配置。
第一步:新建好Spring AOP项目、导入依赖的jar包:

第二步:添加Bean类,和切面类:
IArithmeticCalculator.java接口类
package com.dx.spring.beans.xml; /**
* Description:Addition, subtraction, multiplication, and division
*/
public interface IArithmeticCalculator {
int add(int i, int j); int sub(int i, int j); int multi(int i, int j); int div(int i, int j);
}
ArithmeticCalculatorImpl.java组件
package com.dx.spring.beans.xml;
public class ArithmeticCalculatorImpl implements IArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
@Override
public int multi(int i, int j) {
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
LoggingAspect.java日志切面类:
package com.dx.spring.beans.xml; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class LoggingAspect {
// 声明该方法为一个前置通知:在目标方法开始之前执行
public void beforeMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("before method " + methodName + " with " + args);
} // 声明该方法为一个后置通知:在目标方法结束之后执行(无论方法是否抛出异常)。
// 但是因为当方法抛出异常时,不能返回值为null,因此这里无法获取到异常值。
public void afterMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("after method " + methodName);
} /**
* 声明该方法为一个返回通知:在目标方法返回结果时后执行(当方法抛出异常时,无法执行)
*/
public void afterReturningMethod(JoinPoint joinpoint, Object result) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println(
"after method " + methodName + " with returning " + (result == null ? "NULL" : result.toString()));
} /**
* 定义一个异常通知函数: 只有在方法跑吹异常时,该方法才会执行,而且可以接受异常对象。
*/
public void afterThrowingMethod(JoinPoint joinpoint, Exception ex) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println(
"after method " + methodName + " occurs exception: " + (ex == null ? "NULL" : ex.getMessage()));
} public Object aroundMethod(ProceedingJoinPoint pJoinPoint) throws Exception {
String methodName = pJoinPoint.getSignature().getName();
List<Object> args = Arrays.asList(pJoinPoint.getArgs()); Object result = null;
try {
// 前置通知
System.out.println("before method " + methodName + " with " + args);
// 执行目标方法
result = pJoinPoint.proceed();
// 返回通知
System.out.println("after method " + methodName + " returning " + result);
} catch (Throwable ex) {
// 异常通知
System.out.println("after method " + methodName + " occurs exception: " + ex.getMessage());
throw new Exception(ex);
}
// 后置通知
System.out.println("after method " + methodName);
return result;
}
}
ValidateAspect.java验证切面类:
package com.dx.spring.beans.xml;
public class ValidateAspect {
public void beforeMethod() {
System.out.println("validate...");
}
}
第三步:添加Spring配置文件spring-aop-xml.xml并配置AOP:
前置、后置、返回、异常通知配置:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 定义 Bean -->
<bean id="arithmeticCalculator" class="com.dx.spring.beans.xml.ArithmeticCalculatorImpl"></bean>
<!-- 配置切面的Bean -->
<bean id="loggingAspect" class="com.dx.spring.beans.xml.LoggingAspect"></bean>
<bean id="validateAspect" class="com.dx.spring.beans.xml.ValidateAspect"></bean> <!-- 配置AOP -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut
expression="execution(* com.dx.spring.beans.xml.IArithmeticCalculator.*(..))"
id="pointcut" />
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<!-- 配置前置、后置、返回、异常通知 -->
<aop:before method="beforeMethod" pointcut-ref="pointcut" />
<aop:after method="afterMethod" pointcut-ref="pointcut" />
<aop:after-returning method="afterReturningMethod" pointcut-ref="pointcut" returning="result" />
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex" />
<!-- 配置环绕通知 -->
<!--<aop:around method="aroundMethod" pointcut-ref="pointcut" /> -->
</aop:aspect>
<aop:aspect ref="validateAspect" order="1">
<aop:before method="beforeMethod" pointcut-ref="pointcut" />
</aop:aspect>
</aop:config>
</beans>
环绕配置:
<aop:around method="aroundMethod" pointcut-ref="pointcut" />
第四步:测试
前置、后置、返回、异常通知配置:
此时打印结果:
validate...
before method add with [1, 3]
after method add
after method add with returning 4
4
validate...
before method div with [4, 2]
after method div
after method div with returning 2
2
环绕配置:
此时打印结果:
validate...
before method add with [1, 3]
after method add returning 4
after method add
4
validate...
before method div with [4, 2]
after method div returning 2
after method div
2
Spring(二十):Spring AOP(四):基于配置文件的方式来配置 AOP的更多相关文章
- Spring4学习笔记-AOP(基于配置文件的方式)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shamrock.blog.51cto.com/2079212/1557743 引 ...
- WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]
原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...
- 二十 Spring的事务管理及其API&事务的传播行为,编程式&声明式(xml式&注解式,底层AOP),转账案例
Spring提供两种事务方式:编程式和声明式(重点) 前者需要手写代码,后者通过配置实现. 事务的回顾: 事务:逻辑上的一组操作,组成这组事务的各个单元,要么全部成功,要么全部失败 事务的特性:ACI ...
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...
- Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
多数据源配置也算是一个常见的开发需求,Spring 和 SpringBoot 中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,我还是建议首选分布式数据库中间件 MyCat 去解决相关问 ...
- spring cloud 入门系列七:基于Git存储的分布式配置中心
我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...
- spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config
我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring声明式事务管理(基于Annotation注解方式实现)
在 Spring 中,除了使用基于 XML 的方式可以实现声明式事务管理以外,还可以通过 Annotation 注解的方式实现声明式事务管理. 使用 Annotation 的方式非常简单,只需要在项目 ...
- day20 二十、加密模块、操作配置文件、操作shell命令、xml模块
一.加密模块 1.hashlib模块:加密 ①有解密的加密方式 ②无解密的加密方式:碰撞检查 -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import hash ...
随机推荐
- Gson通过借助TypeToken获取泛型参数的类型的方法(转)
最近在使用Google的Gson包进行Json和Java对象之间的转化,对于包含泛型的类的序列化和反序列化Gson也提供了很好的支持,感觉有点意思,就花时间研究了一下. 由于Java泛型的实现机制,使 ...
- Java IO:同步、非堵塞式IO(NIO)
转载请注明出处:jiq•钦's technical Blog 引言 JDK1.4中引入了NIO,即New IO,目的在于提高IO速度.特别注意JavaNIO不全然是非堵塞式IO(No-Blocking ...
- uva 10154 - Weights and Measures【dp】qi
题意:uva 10154 - Weights and Measures 题意:有一些乌龟有一定的体重和力量,求摞起来的最大高度.力量必须承受其上面包含自己的所有的重量. 分析:先按其能举起来的力量从小 ...
- STM32 TIMER OUTPUT DIAGRAM
- STM32 F4 Clock Sources
STM32 F4 Clock Sources Goal: routing clock sources to the microcontroller output pin (MCO1) High- ...
- ChibiOS/RT 2.6.9 CAN Driver
Detailed Description Generic CAN Driver. This module implements a generic CAN (Controller Area Netwo ...
- .NET 开源Protobuf-net从入门到精通
<.NET 开源Protobuf-net从入门到精通>课程包含以下两个部分: 一..NET 开源Protobuf-net组件[数据存储篇] 本次分享课程包含以下干货知识点: 1.什么是Pr ...
- frame与bounds的区别比较
翻译文档上的 bounds是指这个view在它自己坐标系的坐标和大小 而frame指的是这个view在它superview的坐标系的坐标和大小 区别主要在坐标系这一块. 很明显一个是自己为原点的坐标 ...
- h.264 x.264
这是两个不同类型的东西. x264是视频编码器,H.264是视频编码格式. x264编出来的东西就是h.264的.举个例子来说,x264就好比画图或者photoshop,h.264就好比jpg,bmp ...
- ASP.NET MVC:如何提供 Controller 继承体系使用的 ModelBinder?
背景 Mvc 提供了一种可扩展的模型绑定机制,具体来说就是:将客户端传递的参数按照一定的策略绑定到 action 的参数上,这带来的直接好处就是让 action 的参数支持强类型.一般来说我们有如下方 ...