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学习视频里面一开始都会提到 ...
随机推荐
- LINUX 命令—netstat [简单实用]
1.--当我们在检查程序是否启动或者网络状况的时候 会查看本机活跃的端口,就需要这个命令: |--"netstat – Print network connections, routing ...
- postman安装Postman Interceptor 插件
做后端开发避免不了进行接口调试,但是一般的项目都是前后端分离的,如果把前端代码下到本地,较为费事,这个时候就需要一个可以进行接口调试的工具.Postman就是一个不错的选择. Postman是什么? ...
- 初识ADO.NET
摘要 作为.NET框架最重要的组件之一,ADO.NET扮演着应用程序与数据交互的重要的角色.本文将从宏观的角度来探讨ADO.NET,和大家一起了解ADO.NET来龙去脉以及ADO.NET的主要组成部分 ...
- Web应用程序与Web网站及部署在IIS中
在Visual Studio可以创建 Web 应用程序项目或网站项目.通过选择 新建项目 或 打开项目 创建或打开一个 Web 应用程序项目在Visual Studio 文件 菜单. 通过选择 新建网 ...
- NOIP2013 DAY2题解
DAY2 T1积木大赛 传送门 题目大意:每次可以选区间[l,r]加1,最少选几次,让每个位置有 它应有的高度. 题解:O(n)扫一遍就好了.后一个比前一个的高度低,那么前一个已经把它覆盖了, 如果高 ...
- strftime()和strptime的区别
strftime()是把时间转成规定格式的字符串 strptime()是把字符串转成时间 转载:http://blog.csdn.net/caimouse/article/details/501986 ...
- 在Spring中通过构造自动装配--constructor
在Spring中,可以使用“通过构造自动装配”,实际上是按构造函数的参数类型自动装配. 这意味着,如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么将自动装配. packag ...
- JavaWeb中验证码的实现
在Web程序中,验证码是经常使用的技术之一.Web程序永远面临未知用户和未知程序的探测.为了防止恶意脚本的执行,验证码技术无疑是首选方案之一.本文将讨论如何在JSP和Servlet中使用验证码技术. ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing
题目链接:http://codeforces.com/contest/573/problem/C题目大意:在两行无限长的点列上面画n个点以及n-1条边使得构成一棵树,并且要求边都在同一平面上且除了节点 ...
- 用css来写一个背景图片的切换
代码如下: <!DOCTYPE HTML> <htmllang="en-US"> <head> <meta charset="U ...