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 来实现日志的统一收 ...
随机推荐
- LDA Gibbs Sampling
注意:$\alpha$和$\beta$已知,常用为(和LDA EM算法不同) 1. 为什么可用 LDA模型求解的目标为得到$\phi$和$\theta$ 假设现在已知每个单词对应的主题$z$,则可 ...
- Linux学习之六-Yum命令的使用
详细介绍一下yum命令的用法.如果你是一个Linux的初学者,一定会被软件的安装所困扰过,尽管RPM包解决了一定层度的问题,但有些RPM的包的依赖关系让人很是头疼.而YUM.APT等一些RPM包的管理 ...
- 在magento中发邮件
1. 在system->Configuration->Store Email Addresses中设置General Contact的Sender Name.Sender Email. S ...
- 有关Repeater的事件
Repeater放在Updatepanel中是可以通过右键->属性,双击事件来生成事件的,若能这样的话,那最后是用这种方法吧,最起码不会出错!
- android的二进制和十六进制的相互转换工具类(一):
二进制和十六进制的相互转换工具类: package com.gzcivil.utils; public class BinStr { /** * 将二进制转换成16进制 * @param buf * ...
- 第9课_1_cluster安装
1. 配置网络 vi ifcfg-eth0 ifup ifcfg-eth0 [root@localhost init.d]# ./network restart Shutting down inter ...
- iOS-Core Text 入门
NSTextView和Attribued String 第一次接触苹果系的富文本编程是在写Mac平台上的一个输入框的时候,输入框中的文字可以设置各种样式,并可以在文字中间插入图片,好在Mac的AppK ...
- spring学习参考资料
http://www.cnblogs.com/ooooevan/p/5795456.html http://blog.csdn.net/hongjun1847/article/details/2053 ...
- linux查看与设置主机名
1.设置主机名 通过编辑/etc/sysconfig/network文件中的HOSTNAME字段就可以修改主机名.如下所示: [root@zijuan /]# vim /etc/sysc ...
- Java提高学习之Object(4)
哈希码 问: hashCode()方法是用来做什么的? 答: hashCode()方法返回给调用者此对象的哈希码(其值由一个hash函数计算得来).这个方法通常用在基于hash的集合类中,像java. ...