zoukankan      html  css  js  c++  java
  • spring aop自动代理注解配置之一

    <?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>
    </beans>
    package com.zr.utils;
    
    
    import org.springframework.stereotype.Component;
    
    
    @Component("comp")
    public class Calculate implements Compute{
        
        public int div(int num1,int num2){
            System.out.println("除法");
            return num1/num2;
        }
    }
    package com.zr.utils;
    
    
    
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    
    @Aspect
    @Component("testAspect")
    public class TestAspect {
    
        
        //第一个 * 代表任意修饰符及任意返回值,其中 .. 匹配任意数量的参数.
        @Before("execution(* com.zr.utils.Calculate.div(..))")
        public void methodBefore(){
            System.out.println("执行方法之前");
        }
        //第一个 * 代表public修饰符下任意返回值,第一个 * 代表com.zr.utils.Calculate路径下的任意方法
        @After("execution(public * com.zr.utils.Calculate.*(..))")
        public void methodAfter() {
            System.out.println("执行方法之后");
        }
        
        //匹配第一个参数为 int 类型的方法, .. 匹配任意数量任意类型的参数
        @AfterReturning("execution(public int com.zr.utils.Calculate.*(int,..))")
        public void methodAfterRunning(){
            System.out.println("返回结果之后执行");
        }
        //匹配参数类型为 int, int 类型的方法.
        @AfterThrowing("execution(public int com.zr.utils.Calculate.*(int,int))")
        public void methodAfterThrowing(){
            System.out.println("异常通知, 在方法抛出异常之后执行");
        }
    }
    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("comp");
            int result = c.div(10, 2);
            
            
        }
    }
  • 相关阅读:
    1 < 2 < 3为true, 3 > 2 > 1为false
    我的第五代选择器Icarus
    浮动不换行
    教你游泳,不会游的看了包你学会!!! 分享
    node.js 一个简单的页面输出
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    setTimeout和setInterval的使用
    JS window.open()属性
    车牌识别及验证码识别的一般思路
    苹果开发者账号注册流程
  • 原文地址:https://www.cnblogs.com/lantu1989/p/6295554.html
Copyright © 2011-2022 走看看