zoukankan      html  css  js  c++  java
  • Spring AOP + AspectJ in XML configuration example

    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>
    
  • 相关阅读:
    Pizza Pie Charts – 基于 Snap SVG 框架的响应式饼图
    超好玩!10款神奇的字符图案 & 词汇云生成工具
    『摄影欣赏』15幅迷人的来自世界各地的婴儿照片【组图】
    CSS 魔法系列:纯 CSS 绘制图形(各种形状的钻石)
    【特别推荐】10款唯美浪漫的婚礼 & 结婚纪念网站模板
    25款创新的 PSD 格式搜索框设计素材【免费下载】
    时尚前沿:15个创意的 3D 字体设计艺术作品欣赏
    Resumable.js – 基于 HTML5 File API 的文件上传
    经典设计:17个最有效的学习着陆页设计的例子
    图标集锦:10套免费的社交媒体 & 社交网站图标
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4752405.html
Copyright © 2011-2022 走看看