`
tuoxinquyu
  • 浏览: 13865 次
  • 性别: Icon_minigender_2
  • 来自: 郑州
社区版块
存档分类
最新评论

Spring AOP + AspectJ in XML 配置示例

阅读更多

In this tutorial, we show you how to convert last Spring AOP + AspectJ annotation into XML based configuration.

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>
文章来源:http://www.mkyong.com/spring3/spring-aop-aspectj-in-xml-configuration-example/
分享到:
评论

相关推荐

    spring-aop-aspectj-ctw:用AspectJ编译Spring AOP

    在这个示例中,您可以在一个简单的Spring Boot示例中看到如何使用AspectJ处理节拍时间编织。 示例场景 在此示例中,将发生以下情况: 我们在上调用我们的应用程序 MyRest.startComputation将被调用,但没有控制台...

    spring中aop的xml配置方法实例详解

    AOP的配置方式有2种方式:xml配置和AspectJ注解方式。下面这篇文章主要给大家介绍了关于spring中aop的xml配置方法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。

    spring-loadtime-weaving-example:使用AspectJ的Spring Boot加载时编织示例

    使用AspectJ的Spring Boot加载时编织示例 这是一个用AspectJ编织Spring Boot加载时间的示例。 这是前面的的延续。 加载时间编织 加载时编织是一种二进制编织,其中已编译的Java类在运行时(而不是编译时)作为输入。...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....

    Java的Spring框架下的AOP编程模式示例

    主要介绍了Java的Spring框架下的AOP编程模式示例,文中分别讲到了基于XML和基于@AspectJ的自定义方式,需要的朋友可以参考下

    Spring 2.0 开发参考手册

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...

    Spring中文帮助文档

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    Spring API

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    spring chm文档

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...

    spring security 参考手册中文版

    6.2.1 web.xml配置 50 6.2.2最小的配置 50 6.2.3表单和基本登录选项 52 设置默认的登录目的地 54 6.2.4注销处理 54 6.2.5使用其他身份验证提供程序 55 添加密码编码器 56 6.3高级Web功能 56 6.3.1记得我认证 56 ...

    Spring Security 中文教程.pdf

    2.2.1. 配置web.xml 2.2.2. 最小 &lt;http&gt; 配置 2.2.2.1. auto-config 包含了什么? 2.2.2.2. 表单和基本登录选项 2.2.3. 使用其他认证提供器 2.2.3.1. 添加一个密码编码器 2.3. 高级web特性 2.3.1. ...

    spring完全学习实例(一)

    主要包括Spring的IoC、AOP(注释注入、Schema注入、与AspectJ的集成、有接口和无接口的实现的界面编程)、中间数据访问层(编程事事务管理、声明式事务管理、与Hibernate的集成、与JDO的集成、与JTA的集成、纯Spring...

    SpringSecurity 3.0.1.RELEASE.CHM

    2.2.1. 配置web.xml 2.2.2. 最小 配置 2.2.2.1. auto-config包含了什么? 2.2.2.2. 表单和基本登录选项 2.2.3. 使用其他认证提供器 2.2.3.1. 添加一个密码编码器 2.3. 高级web特性 2.3.1. Remember-Me认证 ...

    Spring Security-3.0.1中文官方文档(翻译版)

    2.2.1. 配置web.xml 2.2.2. 最小&lt;http&gt; 配置 2.2.2.1. auto-config 包含了什么? 2.2.2.2. 表单和基本登录选项 2.2.3. 使用其他认证提供器 2.2.3.1. 添加一个密码编码器 2.3. 高级web 特性 2.3.1. ...

    doodle:A Simple Java MVC Framework。提供Bean容器、Ioc、Aop、MVC功能

    不使用XML配置,完全注解开发 IOC容器,解决循环依赖注入问题 支持多AOP增强,切点支持aspectj表达式 Http请求访问设计图: 示例代码 git clone https://github.com/zzzzbw/doodle.git 或直接下载代码到本地 将项目...

Global site tag (gtag.js) - Google Analytics