zoukankan      html  css  js  c++  java
  • Spring 嵌套AOP 的顺序问题 指定Aspect优先级

    Spring3 M2

    另外所需要的jar

    aopalliance-1.0.jar

    aspectjrt-1.6.8.jar

    aspectjweaver-1.6.8.jar

    cglib-nodep-2.2.3.jar

    package com.epkj.test;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class Aop1 {
    
        public void log(ProceedingJoinPoint point) throws Throwable {
            System.out.println("Aop1.log1()-begion");
            point.proceed();
            System.out.println("Aop1.log1()-end");
        }
    }
    <?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:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
        
        <context:component-scan base-package="com.epkj.*" />
        
        <aop:config>
            <aop:aspect id="asoect1" ref="aop1" order="2">
                <aop:pointcut expression="execution(* com.epkj.test.*.*(..))" id="allMethod"/>
                <aop:around method="log" pointcut-ref="allMethod"/>
            </aop:aspect>
        </aop:config> 
        
        <aop:config>
            <aop:aspect id="asoect2" ref="aop2" order="1">
                <aop:pointcut expression="execution(* com.epkj.test.*.*(..))" id="allMethod"/>
                <aop:around method="log" pointcut-ref="allMethod"/>
            </aop:aspect>
        </aop:config>
        
        <bean id="aop1" class="com.epkj.test.Aop1" />
        <bean id="aop2" class="com.epkj.test.Aop2" />
    </beans>
     <aop:aspect id="asoect2" ref="aop2" order="1">
     order属性用来指定AOP的顺序

    还有其他几种方式
    package com.codeproject.jackie.springrecipesnote.springaop;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.core.Ordered;
    
    /**
     * @author jackie
     * 
     */
    @Aspect
    public class CalculatorValidationAspect implements Ordered{
        private Log log = LogFactory.getLog(this.getClass());
        
        @Before("execution(* *.*(double, double))")
        public void validateBefore(JoinPoint joinPoint) {
            for (Object arg : joinPoint.getArgs()) {
                log.info("validate begins...");
                validate((Double) arg);
            }
        }
    
        private void validate(Double arg) {
            if (arg < 0) {
                throw new IllegalArgumentException("Positive numbers only");
            }
        }
    
        @Override
        public int getOrder() {
            return 0;
        }
    }&nbsp; 
    package com.codeproject.jackie.springrecipesnote.springaop;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.core.annotation.Order;
    
    /**
     * @author jackie
     * 
     */
    @Aspect
    @Order(0)
    public class CalculatorValidationAspect {
        private Log log = LogFactory.getLog(this.getClass());
        
        @Before("execution(* *.*(double, double))")
        public void validateBefore(JoinPoint joinPoint) {
            for (Object arg : joinPoint.getArgs()) {
                log.info("validate begins...");
                validate((Double) arg);
            }
        }
    
        private void validate(Double arg) {
            if (arg < 0) {
                throw new IllegalArgumentException("Positive numbers only");
            }
        }
    }


    
    
  • 相关阅读:
    程序的了解
    Oracle VM VirtualBox虚拟网卡消失解决方法
    YARN 运维、巡检、监控、调优、排障
    HDFS巡检、监控、调优、排障
    Windows CMD命令大全
    [HDU]6356 Glad You Came(ST表)
    [BZOJ] 1019 [SHOI2008]汉诺塔
    树上叶子之间点对距离平方和
    [BZOJ]1026[SCOI2009]windy数
    [计蒜客]A1542 The Maximum Unreachable Node Set
  • 原文地址:https://www.cnblogs.com/daxin/p/3112049.html
Copyright © 2011-2022 走看看