<?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); }
}

spring aop自动代理xml配置的更多相关文章

  1. spring aop自动代理注解配置之二

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. spring aop自动代理注解配置之一

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  4. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  5. 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  6. Spring Aop实例之xml配置

    AOP的配置方式有2种方式:xml配置和AspectJ注解方式.今天我们就来实践一下xml配置方式. 我采用的jdk代理,所以首先将接口和实现类代码附上 package com.tgb.aop; pu ...

  7. Spring AOP 动态代理 缓存

    Spring AOP应用:xml配置及注解实现. 动态代理:jdk.cglib.javassist 缓存应用:高速缓存提供程序ehcache,页面缓存,session缓存 项目地址:https://g ...

  8. 使用BeanNameAutoProxyCreator实现spring的自动代理

    提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置 ...

  9. Spring AOP 注解和xml实现 --转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

随机推荐

  1. mac地址常识及获取

    mac常识: 网卡地址这个概念有点混淆不清.因为实际上有两个地址,mac地址和物理地址,一般说网卡地址我是指物理地址,不知道别人怎么看?物理地址指的是网卡上的存放地址的ROM里的地址,mac地址是这块 ...

  2. linux rhel7下安装python

    1.查看是否已经安装Python Centos7默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用python -V命令查看一下是否安装Pytho ...

  3. 《DSP using MATLAB》示例Example7.17

    代码: M = 40; alpha = (M-1)/2; l = 0:M-1; wl = (2*pi/M)*l; T1 = 0.109021; T2 = 0.59417456; Hrs = [zero ...

  4. 文本溢出显示省略号,CSS未加载时a标签仍可用处理方法

    一.文本溢出打点 (1)单行文本 overflow: hidden; text-overflow:ellipsis; white-space: nowrap; (2)多行文本 overflow : h ...

  5. cookie跨站脚本漏洞解决方案

    近日项目碰到一个跨脚本注入的问题: 这安全测评工具也是厉害了,直接将脚本注入到cookie里头,以前没有碰到这样的情况. 之前写过一篇文章过滤跨脚本注入的问题.<浅谈XSS攻击原理与解决方法&g ...

  6. sqlserver卸载

  7. MinGW安装与环境变量配置和Sublime Text 2搭建C++编译环境

    MinGW安装与环境变量配置 从http://sourceforge.net/projects/mingw/下载MinGW,安装到D:\MinGW.工具集选择安装(之后还可以进行安装卸载):至少需要安 ...

  8. php排序集合

    如果你已经使用了一段时间PHP的话,那么,你应该已经对它的数组比较熟悉了——这种数据结构允许你在单个变量中存储多个值,并且可以把它们作为一个集合进行操作. 经常,开发人员发现在PHP中使用这种数据结构 ...

  9. Vue.js:表单

    ylbtech-Vue.js:表单 1.返回顶部 1. Vue.js 表单 这节我们为大家介绍 Vue.js 表单上的应用. 你可以用 v-model 指令在表单控件元素上创建双向数据绑定. v-mo ...

  10. PHP面向对象深入研究之【了解类】与【反射API】

    了解类 class_exists验证类是否存在 <?php // TaskRunner.php $classname = "Task"; $path = "task ...