基于spring-framework-4.1.7使用aop

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2015年9月5日 23:46:28 星期六

http://fanshuyao.iteye.com/

一、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 星期六

http://fanshuyao.iteye.com/

spring使用aop的更多相关文章

  1. Spring基于AOP的事务管理

                                  Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...

  2. Spring实现AOP的4种方式

    了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“ ...

  3. spring的AOP

    最近公司项目中需要添加一个日志记录功能,就是可以清楚的看到谁在什么时间做了什么事情,因为项目已经运行很长时间,这个最初没有开来进来,所以就用spring的面向切面编程来实现这个功能.在做的时候对spr ...

  4. Spring(五)AOP简述

    一.AOP简述 AOP全称是:aspect-oriented programming,它是面向切面编号的思想核心, AOP和OOP既面向对象的编程语言,不相冲突,它们是两个相辅相成的设计模式型 AOP ...

  5. Spring中AOP原理,源码学习笔记

    一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...

  6. Spring之AOP面向切片

       一.理论基础: AOP(Aspectoriented programming)面向切片/服务的编程,在Spring中使用最多的是对事物的处理.而AOP这种思想在程序中很多地方可以使用的,比如说, ...

  7. 利用CGLib实现动态代理实现Spring的AOP

    当我们用Proxy 实现Spring的AOP的时候, 我们的代理类必须实现了委托类的接口才能实现. 而如果代理类没有实现委托类的接口怎么办? 那么我们就可以通过CGLib来实现 package cn. ...

  8. spring之aop概念和配置

    面向切面的一些概念: 简单说: 连接点就一些方法,在这些方法基础上需要额外的一些业务需求处理. 切入点就是方法所代表的功能点组合起来的功能需求. 通知就是那些额外的操作. 织入就是使用代理实现整个切入 ...

  9. Spring的AOP与代理

    spring 支持两种注入方式: setter/constructor 支持多种配置方式: xml/java5注解/java类配置 支持两种事务管理: 声明性/编程性 实际上上述方式只有一个就能保证系 ...

  10. Spring 实践 -AOP

    Spring 实践 标签: Java与设计模式 AOP引介 AOP(Aspect Oriented Programing)面向切面编程采用横向抽取机制,以取代传统的纵向继承体系的重复性代码(如性能监控 ...

随机推荐

  1. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary

    一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...

  2. SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean

    1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...

  3. [QuickX]xcode运行Quick-cocos2d-x项目时自动更新lua资源文件

    1.项目设置 build settings ->build options ->Scan all source files and Includes = YES 2.加入script (1 ...

  4. 关于v$sql_bind_capture 的问题

    ---先清空shared_pool SQL> alter system flush shared_pool; System altered. SQL> col value_STRING f ...

  5. JavaScript日期时间操作

    <script> var d=new Date();//当前时间 alert(d); var d1=new Date(1992,5,19);//定义一个时间,月份要加1; alert(d1 ...

  6. BZOJ_4196_[NOI2015]_软件包管理器_(树链剖分)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=4196 给出一棵树,树上点权为0或1.u权值为1的条件是从根节点到u路径上的所有点权值都为1.u ...

  7. PowerDesigner使用总结 转

    PowerDesigner使用总结 (友情提示:本博文章欢迎转载,但请注明出处:陈新汉,http://www.blogjava.net/hankchen)一.使用PowerDesigner生成HTML ...

  8. Ubuntu 使用apt-get时提示错误:无法获得锁 /var/lib/dpkg/lock

    推荐博客:http://blog.sina.com.cn/s/blog_5c1450a8010188ju.html Ubuntu 使用apt-get时提示错误:无法获得锁 /var/lib/dpkg/ ...

  9. Bzoj 1598: [Usaco2008 Mar]牛跑步 dijkstra,堆,K短路,A*

    1598: [Usaco2008 Mar]牛跑步 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 427  Solved: 246[Submit][St ...

  10. ubuntu14.04 swap not avalible交换分区不能使用

    系统最近特别卡,打开"System monitor"中的resource发现"swap not avalibe".原来系统每交换分区. 我的是笔记本电脑,存储空间有限.首先我下载磁盘分区工具Gpart ...