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 ...
 
随机推荐
- 接口开发-基于SpringBoot创建基础框架
			
说到接口开发,能想到的开发语言有很多种,像什么Java啊..NET啊.PHP啊.NodeJS啊,太多可以用.为什么选择Java,究其原因,最后只有一个解释,那就是“学Java的人多,人员招聘范围大,有 ...
 - .NET 4.0中使用内存映射文件实现进程通讯
			
操作系统很早就开始使用内存映射文件(Memory Mapped File)来作为进程间的共享存储区,这是一种非常高效的进程通讯手段.Win32 API中也包含有创建内存映射文件的函数,然而,这些函数都 ...
 - dotNetSpider 手记
			
准备工作: 从github上download工程. 安装VS2017. 安装 .net core 2.0. 编译通过. 基础架构: 调度器 Scheduler 从根site开始,向 Downloade ...
 - 详细分析Memcached缓存与Mongodb数据库的优点与作用
			
http://www.mini188.com/showtopic-1604.aspx 本文详细讲下Memcached和Mongodb一些看法,以及结合应用有什么好处,希望看到大家的意见和补充. Mem ...
 - code.google.com/p/log4go 下载失败
			
用 glide 下载 goim 的依赖包时报错,提示: code.google.com/p/log4go 找不到,即下载失败 主要是 code.google.com 网站已关闭导致的, 有人把它 fo ...
 - libxml2.dylb 罗致<libxml/tree.h> 老是找不到头文件
			
libxml2.dylb 导致<libxml/tree.h> 老是找不到头文件 添加了libxml2.dylb的framework ,结果还是引用不了<libxml/tree.h&g ...
 - [SQLite][Error Code] 21 misuse
			
若使用SQLite API時,出現错误代码21(misuse),可能是你的SQLiteConnection同時打開(Open)了兩個相同的Data source,所造成的错误. 解決方法:检查代码 ...
 - 【POI】maven引用POI的依赖,XSSFWorkbook依旧无法使用的问题。
			
maven项目引用POI的jar: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependenc ...
 - .NET:注意 Primitive 这个小坑
			
背景 有个需求,需要递归遍历类型的所有属性(属性的属性),然后对不同的类型做不同的处理,或者只是将类型分为三类:Primitive.Complex 和 Collection.因为 MS 的 Type ...
 - Nginx中文手冊
			
下载 : Nginx 中文手冊 Nginx 常见应用技术指南[Nginx Tips] 第二版 作者:NetSeek http://www.linuxtone.org (IT运维专家网|集群架构|性能调 ...