基于配置文件的方式来配置 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的更多相关文章

  1. Spring4学习笔记-AOP(基于配置文件的方式)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shamrock.blog.51cto.com/2079212/1557743 引 ...

  2. WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]

    原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...

  3. 二十 Spring的事务管理及其API&事务的传播行为,编程式&声明式(xml式&注解式,底层AOP),转账案例

    Spring提供两种事务方式:编程式和声明式(重点) 前者需要手写代码,后者通过配置实现. 事务的回顾: 事务:逻辑上的一组操作,组成这组事务的各个单元,要么全部成功,要么全部失败 事务的特性:ACI ...

  4. Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)

    参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...

  5. Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源

    多数据源配置也算是一个常见的开发需求,Spring 和 SpringBoot 中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,我还是建议首选分布式数据库中间件 MyCat 去解决相关问 ...

  6. spring cloud 入门系列七:基于Git存储的分布式配置中心

    我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...

  7. spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config

    我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...

  8. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring声明式事务管理(基于Annotation注解方式实现)

    在 Spring 中,除了使用基于 XML 的方式可以实现声明式事务管理以外,还可以通过 Annotation 注解的方式实现声明式事务管理. 使用 Annotation 的方式非常简单,只需要在项目 ...

  9. day20 二十、加密模块、操作配置文件、操作shell命令、xml模块

    一.加密模块 1.hashlib模块:加密 ①有解密的加密方式 ②无解密的加密方式:碰撞检查 -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import hash ...

随机推荐

  1. FireDAC 下的 Sqlite [3] - 获取数据库的基本信息

    在空白窗体上添加: TFDConnection, TFDPhysSQLiteDriverLink, TFDGUIxWaitCursor, TMemo procedure TForm1.FormCrea ...

  2. android.os.handler(转)

    android.os.handler相关知识整理 Handler在android里负责发送和处理消息.它的主要用途有: 1)按计划发送消息或执行某个Runnanble(使用POST方法): 2)从其他 ...

  3. LPC43xx SGPIO Slice 示意图

    SGPIO inverted clock qualifier Hi, With bits 6:5 of SGPIO_MUX_CFG the QUALIFIER_MODE is selected (0x ...

  4. 关于STM32数据手册中的定时器信号

    首先,我们可以看到这个图大概有两个不分,一个部分是时钟源,另一个部分则是输入输出 时钟源计数,到CNT计数器,然后根据捕获比较寄存器进行记录或比较.记录或比较有不同的配置. 首先是TI信号TI1 TI ...

  5. 《Go语言实战》摘录:6.2 并发 - goroutine

    6.2 goroutine

  6. linux 内核crash 命令

    https://www.dedoimedo.com/computers/crash-book.html#download

  7. 在ASP.NET Web API中实现CORS(跨域资源共享)

    默认情况下,是不允许网页从不同的域访问服务器资源的,访问遵循"同源"策略的原则. 会遇到如下的报错: XMLHttpRequest cannot load http://local ...

  8. Delphi2010 RTTI + Attribute 简单实现ORM实例

    1.支持ORM,最基础的两个信息是表的信息和字段信息.这两个信息,如果用Attribute 来辅助,代码更简洁和可读性更好.可以把属性名当做真实字段名,也可以将特性里的属性当成真实姓名,再加上字段标题 ...

  9. lufylegend:图形变形2

    下面来详细讲解一下drawtriangles函数的使用方法.并且使用drawtriangles函数实现下面这种处理效果 因为这个方法是从AS3移植而来,所以它的使用方法和AS3基本一致,这里是AS3的 ...

  10. 在xcode5下设置两个viewController跳转——关键是禁用arc

    1.禁用arc 2.然后使用如下代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( ...