代码改变世界

spring aop自动代理xml配置

2017-01-18 11:54  甘雨路  阅读(506)  评论(0编辑  收藏  举报
<?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"
        xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> 
        
        <context:component-scan base-package="com.zr.utils"></context:component-scan>
        
        <!-- <aop:aspectj-autoproxy ></aop:aspectj-autoproxy> -->
        
        <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
        
        <bean id="count" class="com.zr.utils.Calculate"></bean>
        <bean id="testAspect" class="com.zr.utils.TestAspect"></bean>
        <aop:config>
            <aop:aspect id="test" ref="testAspect">
                <aop:pointcut id="calculate" expression="execution(* com.zr.utils.*.*(..))" />
                <aop:before method="methodBefore" pointcut-ref="calculate"/>
                <aop:after method="methodAfter" pointcut-ref="calculate"/>
                <aop:after-returning method="methodAfterRunning" pointcut-ref="calculate" returning="result"/>
                <aop:after-throwing method="methodAfterThrowing" pointcut-ref="calculate" throwing="exception"/>
            </aop:aspect>
        </aop:config> 
        
</beans>
package com.zr.utils;





public class Calculate implements Compute{
    
    public int div(int num1,int num2){
        System.out.println("除法");
        return num1/num2;
    }
}
package com.zr.utils;



import java.util.Arrays;

import org.aspectj.lang.JoinPoint;




public class TestAspect {
    
    
    
    //第一个 * 代表任意修饰符及任意返回值,其中 .. 匹配任意数量的参数.
    public void methodBefore(JoinPoint joinpoint){
        System.out.println("执行方法:"+joinpoint.getSignature().getName()+"之前"+" 参数:"+Arrays.asList(joinpoint.getArgs()));
    }
    //第一个 * 代表public修饰符下任意返回值,第一个 * 代表com.zr.utils.Calculate路径下的任意方法
    public void methodAfter(JoinPoint joinpoint) {
        System.out.println("执行方法:"+joinpoint.getSignature().getName()+"之后");
    }
    
    //匹配第一个参数为 int 类型的方法, .. 匹配任意数量任意类型的参数
    public void methodAfterRunning(JoinPoint joinpoint,Object result){
        System.out.println("返回结果之后执行"+",返回结果:"+result);
    }
    //匹配参数类型为 int, int 类型的方法.
    public void methodAfterThrowing(JoinPoint joinPoint,Exception exception){
        System.out.println("异常通知, 在方法抛出异常之后执行"+",异常日志:"+exception);
    }
}
package com.zr.utils;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {
    
    public static void main(String[] args) {
        
        ApplicationContext ctc = new ClassPathXmlApplicationContext("context.xml");
        
        /*Calculate calculate = (Calculate) ctc.getBean("comp");*/
        Calculate c = (Calculate) ctc.getBean("count");
        int result = c.div(10, 2);
        
        
    }
}