For those don’t like annotation or using JDK 1.4, you can use AspectJ in XML based instead.

Review last customerBo interface again, with few methods, later you will learn how to intercept it via AspectJ in XML file.

package com.mkyong.customer.bo;

public interface CustomerBo {

	void addCustomer();

	String addCustomerReturnValue();

	void addCustomerThrowException() throws Exception;

	void addCustomerAround(String name);
}

1. AspectJ <aop:before> = @Before

AspectJ @Before example.

package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; @Aspect
public class LoggingAspect { @Before("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))")
public void logBefore(JoinPoint joinPoint) {
//...
} }

Equivalent functionality in XML, with <aop:before>.

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @Before -->
<aop:pointcut id="pointCutBefore"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" /> <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> </aop:aspect> </aop:config>

2. AspectJ <aop:after> = @After

AspectJ @After example.

package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After; @Aspect
public class LoggingAspect { @After("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))")
public void logAfter(JoinPoint joinPoint) {
//...
} }

Equivalent functionality in XML, with <aop:after>.

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @After -->
<aop:pointcut id="pointCutAfter"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" /> <aop:after method="logAfter" pointcut-ref="pointCutAfter" /> </aop:aspect> </aop:config>

3. AspectJ <aop:after-returning> = @AfterReturning

AspectJ @AfterReturning example.

package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning; @Aspect
public class LoggingAspect { @AfterReturning(
pointcut = "execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))",
returning= "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
//...
} }

Equivalent functionality in XML, with <aop:after-returning>.

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))" /> <aop:after-returning method="logAfterReturning" returning="result"
pointcut-ref="pointCutAfterReturning" /> </aop:aspect> </aop:config>

4. AspectJ <aop:after-throwing> = @AfterReturning

AspectJ @AfterReturning example.

package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing; @Aspect
public class LoggingAspect { @AfterThrowing(
pointcut = "execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))",
throwing= "error")
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
//...
}
}

Equivalent functionality in XML, with <aop:after-throwing>.

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))" /> <aop:after-throwing method="logAfterThrowing" throwing="error"
pointcut-ref="pointCutAfterThrowing" /> </aop:aspect> </aop:config>

5. AspectJ <aop:after-around> = @Around

AspectJ @Around example.

package com.mkyong.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around; @Aspect
public class LoggingAspect { @Around("execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
//...
} }

Equivalent functionality in XML, with <aop:after-around>.

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @Around -->
<aop:pointcut id="pointCutAround"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))" /> <aop:around method="logAround" pointcut-ref="pointCutAround" /> </aop:aspect> </aop:config>

Full XML example

See complete AspectJ XML based configuration file.

<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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy /> <bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl" /> <!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect"> <!-- @Before -->
<aop:pointcut id="pointCutBefore"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" /> <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> <!-- @After -->
<aop:pointcut id="pointCutAfter"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" /> <aop:after method="logAfter" pointcut-ref="pointCutAfter" /> <!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))" /> <aop:after-returning method="logAfterReturning"
returning="result" pointcut-ref="pointCutAfterReturning" /> <!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))" /> <aop:after-throwing method="logAfterThrowing"
throwing="error" pointcut-ref="pointCutAfterThrowing" /> <!-- @Around -->
<aop:pointcut id="pointCutAround"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))" /> <aop:around method="logAround" pointcut-ref="pointCutAround" /> </aop:aspect> </aop:config> </beans>

Spring AOP + AspectJ in XML configuration example的更多相关文章

  1. Spring AOP + AspectJ annotation example

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...

  2. Spring AOP + AspectJ Annotation Example---reference

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...

  3. 关于 Spring AOP (AspectJ) 该知晓的一切

    关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...

  4. Spring学习(十八)----- Spring AOP+AspectJ注解实例

    我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...

  5. 关于 Spring AOP (AspectJ) 你该知晓的一切

    版权声明:本文为CSDN博主「zejian_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/javazej ...

  6. 关于 Spring AOP (AspectJ) 你该知晓的一切 (转)

    出处:关于 Spring AOP (AspectJ) 你该知晓的一切

  7. Spring AOP 注解和xml实现 --转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  8. Spring AOP AspectJ

    本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某 ...

  9. SSM-Spring-18:Spring中aspectJ的XML版

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- aspectJ的xml版是开发中最常用的: 下面直接已案例入手,毕竟繁琐的日子不多了 案例:两个接口,俩个实现 ...

随机推荐

  1. 好!recover-binary-search-tree(难)& 两种好的空间O(n)解法 & 空间O(1)解法

    https://leetcode.com/mockinterview/session/result/xyc51it/https://leetcode.com/problems/recover-bina ...

  2. ha_innobase::rnd_next

    /*****************************************************************//** Reads the next row in a table ...

  3. PHPnow 升级后 PHP不支持GD、MySQL

    来自http://tunps.com/php-unsupport-gd-and-mysql-after-upgrade-phpnow 最近磁盘格式化误操作后,最近两天都在忙于数据恢复,现在才恢复正常. ...

  4. 【.NET应用技巧】Asp.NET MVC 4 设置IIS下调试

    [环境] VS 2012  IIS7.5 [问题] MVC项目在创建时和APS.NET不同,不能够选择服务器类型,不能够直接把项目创建到IIS上. 如果在项目中直接更改属性,更换调试服务器类型,会报错 ...

  5. WDF模型驱动程序开发

    WDF驱动程序开发 1. 引言 设备驱动程序是硬件设备连接到计算机系统的软件接口,任何设备都必须有相应的驱动程序才能在计算机系统上正常工作.设备驱动程序的优劣直接关系到整个系统的性能和稳定性,因此,设 ...

  6. JavaScript备忘录-闭包

    var arr = new Array(); function Person() { for (var i = 0; i < 10; i++) { //要记住,这个属性函数申明,只有立即执行才会 ...

  7. printk 驱动调试

    驱动的调试,printk()添加调试信息 printk相当于printf的孪生姐妹,它们一个运行在用户态,另一个则在内核态. 需要包含<linux/device.h>或者<linux ...

  8. Struts2的OGNL标签详解

    一.Struts2可以将所有标签分成3类: UI标签:主要用于生成HTML元素的标签. 非UI标签:主要用于数据库访问,逻辑控制等标签. Ajax标签:用于Ajax支持的标签. 对于UI标签,则有可以 ...

  9. Egret应用开发实践(01) Egret与WebPack

    Egret Egret引擎是一款使用TypeScript语言构建的开源免费的移动游戏引擎.Egret仅是纯粹的使用TypeScript语言来开发游戏,开发后可以使用Egret来打包为HTML5网页游戏 ...

  10. .NET/ASP.NET Routing路由(深入解析路由系统架构原理)http://wangqingpei557.blog.51cto.com/1009349/1312422

    阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模型的入口 4.ASP.NET Routing 路由对象模型的内部结构 4 ...