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 来实现日志的统一收 ...
随机推荐
- C4.5较ID3的改进
1.ID3选择最大化Information Gain的属性进行划分 C4.5选择最大化Gain Ratio的属性进行划分 规避问题:ID3偏好将数据分为很多份的属性 解决:将划分后数据集的个数考虑 ...
- 滴滴专车司机苹果手机ios客户端可以下载了
申请过滴滴专车司机的朋友都知道,滴滴专车就是滴滴打车的司机端,但是只有安卓的,一直没有苹果IOS的版本,很多申请通过审核的车主恼于没有IOS版本而暂无法使用滴滴专车司机客户端,也就意味着不能上线接单. ...
- Java中Return和Finally运行顺序的实现
以下这段代码的运行结果是如何的呢? [java] view plaincopyprint? publc int test(){ int x; try{ ; return x; }catch(Excep ...
- sql 去除结尾的回车或者换行
CREATE FUNCTION REMOVE_ENTER (@DESC VARCHAR(8000))RETURNS VARCHAR(8000)ASBEGIN DECLARE @STR VARCHAR( ...
- Mschart绘制图表之X轴为时间的设置方式
最近使用C#开发图表,比较了DirectorChart,DontNetCharting,TeeChart,最终选用微软的mschart开发,对于X轴作为时间轴探索了好久,终于实现了想要的效果. 界面效 ...
- java打包/命令行方式运行jar(命令行进行程序测试)
public class Testtmp { public static void main(String[] args) { // TODO Auto-generated method stub f ...
- TCP/IP详解之:ARP协议 和 RARP协议
ARP功能:从逻辑internet地址(IP地址)到对应的物理硬件地址(以太网地址)之间的转换 ARP工作原理: (1)首先每个主机都会在自己的ARP缓冲区中建立一个ARP列表,以表示IP和MAC间的 ...
- IOS优秀博客
链接地址:http://www.cnblogs.com/keithmoring/p/4155264.html 剑心的博客信息量很大,适合查阅和入门,学习完,你差不多就可以出山了,还有作为复习IOS的一 ...
- C/C++ 结构体成员在内存中的对齐规则(转载)
这几天在看王艳平的<windows 程序设计>,第5章讲解了MFC框架是怎么管理窗口句柄到窗口实例之间的映射,用到了两个类CPlex和CMapPtrToPtr,用于管理内存分配的类(避免因 ...
- jsp获取枚举的值
Struts2的Action传回页面一个list,页面迭代这个list,获取下拉框的值,获取过来是枚举类型. 在jsp页面获取枚举的常量值和枚举的值的例子如下: jsp页面: <td >状 ...