<?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. Hibernate之mappedBy【必读】

    [http://www.cnblogs.com/redcoatjk/p/4236445.html] 一.mappedBy 单向关系不需要设置该属性,双向关系必须设置,避免双方都建立外键字段 数据库中1 ...

  2. js中使用分号的情况

  3. ASP.NET 2.0缓存

    MSDN上缓存概述: http://msdn2.microsoft.com/zh-cn/library/726btaeh(VS.80).aspx 一.页输出缓存 1.设置 ASP.NET 页缓存的两种 ...

  4. 关于bonecp和QuerRunner

    之前一直以为boneCP和QueryRunner是绑定的,但是其实不是,后者来自于commons-dbUtils,BoneCP就是负责连接池. while preparing SQL: UPSERT ...

  5. sar 命令

    sar 命令使用详解 1.使用sar命令查看网络流量(每两秒显示一次,共查看3次): [root@localhost ~]# sar -n DEV 2 3Linux 2.6.32-431.el6.x8 ...

  6. 如何在已经安装好的Nginx上增加新模块

    学习资源: https://blog.csdn.net/dxm2025/article/details/41149865 https://blog.csdn.net/qq_36663951/artic ...

  7. nginx安装及编译参数详解

    1.centos下Yum安装 Nginx yum list|grep nginx 发现没有可用的结果通过创建下面的文件在系统中添加nginx仓库的yum配置vi /etc/yum.repos.d/ng ...

  8. 华为公司内部培训资料_介绍RTSP的消息、信令等

    https://wenku.baidu.com/view/b10415dabd64783e08122b9c.html

  9. 【转】JMeter中对于Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  10. Rest之路 - 介绍篇

    What is REST ? REST 是 REpresentational State Transfer 的缩写.是一种基于HTTP协议来进行进行数据交换的web标准框架.她的思想是:视组件为资源. ...