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 java.util.Arrays; import org.aspectj.lang.JoinPoint;
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.stereotype.Component; @Aspect
@Component("testAspect")
public class TestAspect { @Pointcut("execution(* com.zr.utils.Calculate.*(..))")
private void testPointcut(){} //第一个 * 代表任意修饰符及任意返回值,其中 .. 匹配任意数量的参数.
@Before("testPointcut()")
public void methodBefore(JoinPoint joinpoint){
System.out.println("执行方法:"+joinpoint.getSignature().getName()+"之前"+" 参数:"+Arrays.asList(joinpoint.getArgs()));
}
//第一个 * 代表public修饰符下任意返回值,第一个 * 代表com.zr.utils.Calculate路径下的任意方法
@After(value="testPointcut()")
public void methodAfter(JoinPoint joinpoint) {
System.out.println("执行方法:"+joinpoint.getSignature().getName()+"之后");
} //匹配第一个参数为 int 类型的方法, .. 匹配任意数量任意类型的参数
@AfterReturning(pointcut="testPointcut()",returning="result")
public void methodAfterRunning(JoinPoint joinpoint,Object result){
System.out.println("返回结果之后执行"+",返回结果:"+result);
}
//匹配参数类型为 int, int 类型的方法.
@AfterThrowing(pointcut="testPointcut()",throwing="exception")
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("comp");
int result = c.div(10, 2); }
}
spring aop自动代理注解配置之二的更多相关文章
- spring aop自动代理注解配置之一
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring aop自动代理xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- Spring AOP 动态代理 缓存
Spring AOP应用:xml配置及注解实现. 动态代理:jdk.cglib.javassist 缓存应用:高速缓存提供程序ehcache,页面缓存,session缓存 项目地址:https://g ...
- 使用BeanNameAutoProxyCreator实现spring的自动代理
提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置 ...
- Hibernate 延迟加载的代理模式 和 Spring AOP的代理模式
Hibernate 延迟加载的代理模式 和 Spring AOP的代理模式 主题 概念 Hibernate 延迟加载的代理模式 Spring AOP的代理模式 区别和联系 静态代理和动态代理 概念 代 ...
- 运用Spring Aop,一个注解实现日志记录
运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...
随机推荐
- [转]MFC 调用 printf 输出
摘自:http://blog.csdn.net/miyunhong/article/details/6704121 #include <io.h> #include <fcntl.h ...
- centos7怎么把语言切换成英语
一.简介 在Linux的系统中经常碰到字符集导致的错误,本文总结了设置修改系统语言环境的方法步骤. 二.操作步骤 执行如下指令,查看当前使用的系统语言 echo $LANG 执行如下指令,查看系统安装 ...
- Linux 下安装composer
1.下载composer.phar文件. 2.将composer.phar文件上传linux. 3.执行 php composer.phar 4.全局安装:mv composer.phar /usr/ ...
- 【Swift】 - 函数(Functions)总结 - 比较 与 C# 的异同
1.0 函数的定义与调用( Defining and Calling Functions ) 习惯了C#了语法,看到下面的这样定义输入参数实在感到非常别扭,func 有点 Javascript的感觉, ...
- Ambari client
在研究如何修改YARN的资源池的时候,发现了Hortwork在github上面开源了一个Ambari Client: https://github.com/apache/ambari/tree/tru ...
- 洛谷2943 [USACO09MAR]清理Cleaning Up——转变枚举内容的dp
题目:https://www.luogu.org/problemnew/show/P2943 一下想到n^2.然后不会了. 看过TJ之后似乎有了新的认识. n^2的冗余部分在于当后面那部分的种类数一样 ...
- RHEL6 64位ASM方式安装oracle 11gR2(一)
本文转载自 http://vnimos.blog.51cto.com/2014866/1221361 一.安装前的准备 1.1 确定操作系统环境 1 2 3 4 5 6 7 8 9 10 11 12 ...
- TI c6657开发资源
TI 官方论坛:英文 中文 http://software-dl.ti.com/processor-sdk-rtos/esd/docs/latest/rtos/index.html http://e ...
- 求小球反弹高度,及落地过程中经过的路程~~~java代码
总结:这种思想,不是一想就突然出现在脑海里的 package com.c2; //题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半: //再落下,求它在 第10次落地时,共经过多少米?第 ...
- random类类型
random r=new random(): int shu=r.next(3):非负数