spring使用aop
基于spring-framework-4.1.7使用aop
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
蕃薯耀 2015年9月5日 23:46:28 星期六
一、spring中aop的使用需要的jar包:
1、aopalliance.jar
2、aspectjweaver-1.6.12.jar
3、commons-io-2.4.jar
4、commons-logging-1.2.jar
5、spring-aop-4.1.7.RELEASE.jar
6、spring-aspects-4.1.7.RELEASE.jar
7、spring-beans-4.1.7.RELEASE.jar
8、spring-context-4.1.7.RELEASE.jar
9、spring-core-4.1.7.RELEASE.jar
10、spring-expression-4.1.7.RELEASE.jar
备注:不需要使用apache中aspectj的jar包:aspectj-1.8.6.jar
二、springAop.xml配置
1、配置扫描包,把aop执行java类(AopLogging.java)用@Component注解,
然后用注解@Aspect声明该类为aop使用方式。
<context:component-scan base-package="com.spring.aop.*">
</context:component-scan>
2、接着声明使用aop代码
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
三、在java类(AopLogging.java)中的方法使用注解声明通知。
通知有:
1、@Before 前置通知
2、@After 后置通知
3、@AfterReturning 结果通知
4、@AfterThrowing 异常通知
5、@Around 环绕通知(其实此通知为上面4个通知的集合)
然后在通知注解后添加“切点”
@Before("execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))")
@After("execution(* com.spring.aop.service.impl.CalculationServiceImpl.*)")
切点声明,即定义要使用aop的类或者类中的方法,可以用*来代替
注:附件中Java项目为无Jar包导出,需要在src目录下新建立一个lib文件夹,把jar放进去,
然后add to build path
下面为aop的主要代码:
@Component
@Aspect
public class AopLogging { /**
* 前置通过,方法执行前执行
* @param joinpoint(org.aspectj.lang.JoinPoint;)
*/
@Before("execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))")
public void beforeMethod(JoinPoint joinpoint){
System.out.println("---------------the method: "+ joinpoint.getSignature().getName() + " is start.---------------");
System.out.println("【@Before】the method called "+ joinpoint.getSignature().getName() + "'s args is "+ Arrays.asList(joinpoint.getArgs()));
} /**
* @After 后置通知,不管程序有没有错,最后都会执行
* @param joinpoint
*/
@After("execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))")
public void afterMethod(JoinPoint joinpoint){
System.out.println("【@After】---------------the method: "+ joinpoint.getSignature().getName() + " is end.---------------");
} /**
* @AfterReturning 结果通知,只有程序正常执行后才会返回结果通知
* @param joinpoint
* @param obj 对应returning="obj"的obj,名称一样
*/
@AfterReturning(value="execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))",
returning="obj")
public void returnMethod(JoinPoint joinpoint, Object obj){
System.out.println("【@AfterReturning】the method called "+ joinpoint.getSignature().getName() + "'s result is " + obj);
} /**
* @AfterThrowing 异常通知,程序产生异常后(符合异常抓取规则)执行
* @param joinpoint
* @param obj 对应throwing="obj"的obj,名称一样
*/
@AfterThrowing(value="execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))",
throwing="obj")
public void throwingMethod(JoinPoint joinpoint, Object obj){
System.out.println("【@AfterThrowing】the method called "+ joinpoint.getSignature().getName() + " is throw Exeception:" + obj);
} }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
蕃薯耀 2015年9月5日 23:46:28 星期六
spring使用aop的更多相关文章
- Spring基于AOP的事务管理
Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...
- Spring实现AOP的4种方式
了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“ ...
- spring的AOP
最近公司项目中需要添加一个日志记录功能,就是可以清楚的看到谁在什么时间做了什么事情,因为项目已经运行很长时间,这个最初没有开来进来,所以就用spring的面向切面编程来实现这个功能.在做的时候对spr ...
- Spring(五)AOP简述
一.AOP简述 AOP全称是:aspect-oriented programming,它是面向切面编号的思想核心, AOP和OOP既面向对象的编程语言,不相冲突,它们是两个相辅相成的设计模式型 AOP ...
- Spring中AOP原理,源码学习笔记
一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...
- Spring之AOP面向切片
一.理论基础: AOP(Aspectoriented programming)面向切片/服务的编程,在Spring中使用最多的是对事物的处理.而AOP这种思想在程序中很多地方可以使用的,比如说, ...
- 利用CGLib实现动态代理实现Spring的AOP
当我们用Proxy 实现Spring的AOP的时候, 我们的代理类必须实现了委托类的接口才能实现. 而如果代理类没有实现委托类的接口怎么办? 那么我们就可以通过CGLib来实现 package cn. ...
- spring之aop概念和配置
面向切面的一些概念: 简单说: 连接点就一些方法,在这些方法基础上需要额外的一些业务需求处理. 切入点就是方法所代表的功能点组合起来的功能需求. 通知就是那些额外的操作. 织入就是使用代理实现整个切入 ...
- Spring的AOP与代理
spring 支持两种注入方式: setter/constructor 支持多种配置方式: xml/java5注解/java类配置 支持两种事务管理: 声明性/编程性 实际上上述方式只有一个就能保证系 ...
- Spring 实践 -AOP
Spring 实践 标签: Java与设计模式 AOP引介 AOP(Aspect Oriented Programing)面向切面编程采用横向抽取机制,以取代传统的纵向继承体系的重复性代码(如性能监控 ...
随机推荐
- 用JAVA 查询 Active Directory(AD)
Required Details LDAP address (For e.g.: myjeeva.com or IP of the Domain Controller/Global Catalog[G ...
- Windows下Redis中RedisQFork位置调整
redis-server.exe redis.windows.conf 使用上面命令启动redis服务的时候报了以下错误信息: The Windows version of Redis allocat ...
- Android ActivityManagerService 基本构架详解
学习AmS有段时日了,总结下,也好梳理一下自己的思路.小兵一个,有些地方理解不对,大家可以互相讨论,交流才有进步吗~~~ AmS可以说是Android上层系统最核心的模块之一,其主要完成管理应用进程的 ...
- 【HDOJ】1198 Farm Irrigation
其实就是并查集,写麻烦了,同样的代码第一次提交wa了,第二次就过了. #include <stdio.h> #include <string.h> #define MAXNUM ...
- WebLogic12c 注册windows系统服务
1.确认操作系统环境变量中的JAVA_HOME=D:\Oracle\Middleware\jdk160_29与安装部署的位置保持一致: 2.编辑D:\Oracle\Middleware\wlserve ...
- [C#]网络编程系列专题二:HTTP协议详解
转自:http://www.cnblogs.com/zhili/archive/2012/08/18/2634475.html 我们在用Asp.net技术开发Web应用程序后,当用户在浏览器输入一个网 ...
- 外部exe窗体嵌入winform
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...
- java基础(二十二)线程
这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...
- java获取天气预报的信息
运行效果: 主要功能: 1,jsp页面输入省份和城市 根据条件获取当地的天气信息 2,java代码 利用第三方的省份和城市的路径地址 本工程主要实现java获取天气预报的信息步骤1,创建工程weath ...
- Bzoj 1046: [HAOI2007]上升序列 二分,递推
1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3671 Solved: 1255[Submit][Stat ...