spring aop简单日志实例
转载自:http://www.blogjava.net/laoding/articles/242611.html
一直就用spring的IOC,遗憾spring的另一重要组成部分AOP却没用过,所以近几天抽空研究了下AOP,学了些东西,在这里记录下spring2.0的aop配置,以一个简单的记录日志的实例来说明,先介绍下用XMLSchema来配置,下一篇介绍annotation配置,废话不多说,开始吧
先新建个web工程,将spring的包加进去,为方便就把全部的jar包加进去。
先来看个接口,很简单就两个方法
public String print(String name);
public String sleep(String name);
}
接下来是实现类
public String print(String name){
String result="hello " + name;
System.out.println(result);
return result;
}
public String sleep(String name){
String result=name+" is sleep now";
System.out.println(result);
return result;
}
}
下面是所要织入的代码,也就是我们要用来记录日志的
public void getLog(ProceedingJoinPoint joinpoint) throws Throwable {
String reslut = (String)joinpoint.proceed();
//这里是记录日志的
System.out.println("result==="+reslut);
}
}
再来看spring配置文件,没有注释的很清楚,可以去网上查查
<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:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<!--这个bean是作为切面 -->
<bean id="log" class="spring2aop.GetLog"></bean>
<!--
注意这里:expression="execution(* spring2aop.*.print*(..))"
括号里面第一个*号代表返回值 接下来 spring2aop.*. 是你要切入的代码的大概路径,这里为什么用大概路径来形容呢
因为这里的意思是符合以spring2aop的路径都会作为选择的对象,也不详细介绍,查下就明白了, print*(..)是指
方法名以print开头的都符合,括号里面的 .. 表示参数是随意的都可以。
-->
<aop:config>
<aop:aspect ref="log">
<aop:pointcut id="printMethods" expression="execution(* spring2aop.*.print*(..))"/>
<aop:after-returning method="getLog" pointcut-ref="printMethods" returning="retVal"/>
</aop:aspect>
</aop:config>
<aop:config>
<aop:aspect ref="log">
<aop:pointcut id="sleepMethods" expression="execution(* spring2aop.*.sle*(..))"/>
<aop:after-returning method="getLog" pointcut-ref="sleepMethods" returning="retVal"/>
</aop:aspect>
</aop:config>
<!--要织入代码的bean-->
<bean id="print" class="spring2aop.SystemPrint"></bean>
</beans>
测试类:
/**
* @Description 方法实现功能描述
* @param args
* void
* @throws 抛出异常说明
*/
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext(
"applicationContext20.xml");
Print t =(Print)act.getBean("print");
t.print("ding");
System.out.println("-----------------");
t.sleep("laoding");
}
}
运行这个类,得到如下结果:
hello ding
hello ding
result===hello ding
-----------------
laoding is sleep now
laoding is sleep now
result===laoding is sleep now
这里的hello ding 打印了两次,不用担心,这是因为执行到getLog切面类的
String reslut = (String)joinpoint.proceed();这句代码的时候再执行了一次,这句代码是取回
返回结果的,可以设置个断点来测试下好了这里就输出的result就是记录的日志,当然
这里只是个很简单的实现,但是很简单的实现却很容易说清楚原理。
spring aop简单日志实例的更多相关文章
- Spring AOP进行日志记录
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- Spring AOP进行日志记录,管理
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...
- Spring AOP 完成日志记录
Spring AOP 完成日志记录 http://hotstrong.iteye.com/blog/1330046
- 使用Spring AOP 实现日志管理(简单教程)
有时候,我们在做项目时会遇到这样的需求: 给XXX.java中的所有方法加上指定格式的日志输出. 针对这种指定类.或者指定方法进行共性操作的功能,我们完全可以使用Spring AOP来实现. 本文使用 ...
- spring AOP的注解实例
上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...
- Spring AOP 简单入门笔记 (转)
分享一个自己写的最为简单的Spring AOP的应用,其实,本人也是学习Spring不久,只是把一些个人的理解分享下,供参考.可能很多人刚开始不太理解到底啥是AOP,其实它也是相对 OOP来说的,类似 ...
- TinyFrame再续篇:整合Spring AOP实现日志拦截
上一篇中主要讲解了如何使用Spring IOC实现依赖注入的.但是操作的时候,有个很明显的问题没有解决,就是日志记录问题.如果手动添加,上百个上千个操作,每个操作都要写一遍WriteLog方法,工作量 ...
- spring aop实现日志收集
概述 使用spring aop 来实现日志的统一收集功能 详细 代码下载:http://www.demodashi.com/demo/10185.html 使用spring aop 来实现日志的统一收 ...
随机推荐
- 普林斯顿大学算法课 Algorithm Part I Week 3 自我总结
要熟练掌握比较器Comparator public final Comparator<T> MY_COMPARATOR = new myComparator(); //定义比较器 .... ...
- Centos6.5升级gcc for qt5.3.1
1.升级GCC CentOS6.5内置的GCC版本为4.4,而Qt5.2.1则需要4.8.2的支持(支持C++ 11特性),因此,必须先升级GCC wget http://ftp.tsukuba.wi ...
- 【玩转Ubuntu】01. Ubuntu上配置JDK
一.安装JDK 提示:这里我们使用jdk1.6,因为android开发要求使用1.6.如果不信你可以打开android studio,它会提示你选择JDK6的路径 下载地址: http://www.o ...
- Mysql 计算时间间隔函数
#计算两个时间的间隔 #计算间隔天数 select TIMESTAMPDIFF(day,'2014-06-01',date(now())) #计算间隔月数 select TIMESTAMPDIFF(m ...
- CSS 垂直居中的5种实现方法
利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法.我下面说明一下我看到的好的方法和怎么来创建一个好的居中网站. 使用 CSS 实现垂直居中并不容易.有些 ...
- 网站图片列表动态显示、根据屏幕宽度动态设置DIV的CSS样式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
原因:缺少commons-pool-X.jar包,到http://commons.apache.org/proper/commons-pool/download_pool.cgi下载后引入即可(地址可 ...
- poj2891--扩展欧几里德算法
/* 该题使用的是扩展欧几里德算法,求模线性同余方程: 分析题目:以题目输出结果为例 ,要求得到一个整数X可以满足 X % a = r,a,r,为数组名: 设数组元素为两个时, 列出方程:X % a1 ...
- C语言队列的实现
队列是常用的数据结构之一,下面给出一个链式队列的实现: 头文件Queue.h #ifndef Queue_H #define Queue_H typedef int Item; typedef str ...
- 创建、更新、删除文档。 --- Mongodb权威指南阅读。
插入文档: db.foo.insert({ "key" : "value"}); 使用insert插入一个数据,文档中如果没有_id 会自动给文档增加_id. ...