Spring AOP进行日志记录,管理
在java开发中日志的管理有很多种。我一般会使用过滤器,或者是Spring的拦截器进行日志的处理。如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个方法的调用。然后进行日志记录。使用过滤器的好处是可以自己选择性的对某一些方法进行过滤,记录日志。但是实现起来有点麻烦。
另外一种就是使用Spring的AOP了。这种方式实现起来非常简单,只要配置一下配置文件就可以了。可是这种方式会拦截下所有的对action的每个操作。使得效率比较低。不过想做详细日志这个方法还是非常好的。下面我就介绍一下使用Spring AOP进行日志记录的方式。
第一种。Spring AOP对普通类的拦截操作
首先我们要写一个普通类,此类作为日志记录类。 比如
- package chen.hui.log
- public classs MyLog{
- //在类里面写方法,方法名诗可以任意的。此处我用标准的before和after来表示
- public void before(){
- System.out.println("被拦截方法调用之前调用此方法,输出此语句");
- }
- public void after(){
- System.out.println("被拦截方法调用之后调用此方法,输出此语句");
- }
- }
- 其次我们在写一个类作为被拦截类(Spring的AOP就是拦截这个类里面的方法)
- package chen.hui.log
- public class Test{//此类中方法可以写任意多个。我只写一个
- public void test(){
- Sytem.out.println("测试类的test方法被调用");
- }
- }
最后进行配置文件的编写。在Spring的配置文件中我们需要进行几句话的配置
- <bean id="testLog" class="chen.hui.log.MyLog"></bean> <!--将日志类注入到bean中。-->
- <aop:config>
- <aop:aspect id="b" ref="testLog"><!--调用日志类-->
- <aop:pointcut id="log" expression="execution(* chen.hui.log.*.*(..))"/><!--配置在log包下所有的类在调用之前都会被拦截-->
- <aop:before pointcut-ref="log" method="before"/><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的before方法-->
- <aop:after pointcut-ref="log" method="after"/>><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的after方法-->
- </aop:aspect>
- </aop:config>
到此处整个程序完成,在MyLog类里面的before和after方法添加日志逻辑代码就可以完成日志的管理。以上是对普通类的管理,如果只想拦截某一个类。只要把倒数第二个 * 改成类名就可以了。
第二:使用Spring AOP对action做日志管理
如果是想拦截action对action做日志管理,基本和上面差不多,但是要注意。以下几点
首先还是要写一个普通类,不过此类中的方法需要传入参数。 比如
- package chen.hui.log
- import org.aspectj.lang.JoinPoint;
- public classs MyLog{
- //在类里面写方法,方法名诗可以任意的。此处我用标准的before和after来表示
- //此处的JoinPoint类可以获取,action所有的相关配置信息和request等内置对象。
- public void before(JoinPoint joinpoint){
- joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
- System.out.println("被拦截方法调用之前调用此方法,输出此语句");
- }
- public void after(JoinPoint joinpoint){
- System.out.println("被拦截方法调用之后调用此方法,输出此语句");
- }
- }
其次我们在写一个action类作为被拦截类(Spring的AOP就是拦截这个类里面的方法)
- package chen.hui.log
- public class LoginAction{//此类中方法可以写任意多个。我只写一个
- public void test(){
- Sytem.out.println("测试类的test方法被调用");
- }
- }
最后进行配置文件的编写。在Spring的配置文件中我们需要进行几句话的配置
- <bean id="testLog" class="chen.hui.log.MyLog"></bean> <!--将日志类注入到bean中。-->
- <aop:config>
- <aop:aspect id="b" ref="testLog"><!--调用日志类-->
- <aop:pointcut id="log" expression="execution(* chen.hui.log.*.*(..))"/><!--配置在log包下所有的类在调用之前都会被拦截-->
- <aop:before pointcut-ref="log" method="before"/><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的before方法-->
- <aop:after pointcut-ref="log" method="after"/>><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的after方法-->
- </aop:aspect>
- </aop:config>
除了参数外其他地方基本和普通类相似。
需要注意的是:普通类可以监控单一的类,而action在配置文件中只能到包名而不能到action的类名。不然会报错。就是说如果要记录日志就要记录所有的action而不能记录其中一个,这是我试了好久得出的结果。
.实现登陆和日志管理(使用Spring AOP
1)LoginService LogService TestMain
2)用Spring 管理 LoginService 和 LogService 的对象
3)确定哪些连接点是切入点,在配置文件中
4)将LogService封装为通知
5)将通知植入到切入点
6)客户端调用目标
- <aop:config>
- <aop:pointcut expression="execution(* cn.com.spring.service.impl.*.*(..))" id="myPointcut"/>
- <!--将哪个-->
- <aop:aspect id="dd" ref="logService">
- <aop:before method="log" pointcut-ref="myPointcut"/>
- </aop:aspect>
- </aop:config>
execution(* * cn.com.spring.service.impl.*.*(..))
1)* 所有的修饰符
2)* 所有的返回类型
3)* 所有的类名
4)* 所有的方法名
5)* ..所有的参数名
1.ILoginService.java
- package cn.com.spring.service;
- public interface ILoginService {
- public boolean login(String userName, String password);
- }
2.LoginServiceImpl.java
- package cn.com.spring.service.impl;
- import cn.com.spring.service.ILoginService;
- public class LoginServiceImpl implements ILoginService {
- public boolean login(String userName, String password) {
- System.out.println("login:" + userName + "," + password);
- return true;
- }
- }
3.ILogService.java
- package cn.com.spring.service;
- import org.aspectj.lang.JoinPoint;
- public interface ILogService {
- //无参的日志方法
- public void log();
- //有参的日志方法
- public void logArg(JoinPoint point);
- //有参有返回值的方法
- public void logArgAndReturn(JoinPoint point,Object returnObj);
- }
4.LogServiceImpl.java
- package cn.com.spring.service.impl;
- import org.aspectj.lang.JoinPoint;
- import cn.com.spring.service.ILogService;
- public class LogServiceImpl implements ILogService {
- @Override
- public void log() {
- System.out.println("*************Log*******************");
- }
- //有参无返回值的方法
- public void logArg(JoinPoint point) {
- //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
- Object[] args = point.getArgs();
- System.out.println("目标参数列表:");
- if (args != null) {
- for (Object obj : args) {
- System.out.println(obj + ",");
- }
- System.out.println();
- }
- }
- //有参并有返回值的方法
- public void logArgAndReturn(JoinPoint point, Object returnObj) {
- //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
- Object[] args = point.getArgs();
- System.out.println("目标参数列表:");
- if (args != null) {
- for (Object obj : args) {
- System.out.println(obj + ",");
- }
- System.out.println();
- System.out.println("执行结果是:" + returnObj);
- }
- }
- }
5.applicationContext.java
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <bean id="logService" class="cn.com.spring.service.impl.LogServiceImpl"></bean>
- <bean id="loginService" class="cn.com.spring.service.impl.LoginServiceImpl"></bean>
- <aop:config>
- <!-- 切入点 -->
- <aop:pointcut
- expression="execution(* cn.com.spring.service.impl.LoginServiceImpl.*(..))"
- id="myPointcut" />
- <!-- 切面: 将哪个对象中的哪个方法,织入到哪个切入点 -->
- <aop:aspect id="dd" ref="logService">
- <!-- 前置通知
- <aop:before method="log" pointcut-ref="myPointcut" />
- <aop:after method="logArg" pointcut-ref="myPointcut">
- -->
- <aop:after-returning method="logArgAndReturn" returning="returnObj" pointcut-ref="myPointcut"/>
- </aop:aspect>
- </aop:config>
- </beans>
6.TestMain.java
- public class TestMain {
- public static void testSpringAOP(){
- ApplicationContext ctx = new ClassPathXmlApplicationContext("app*.xml");
- ILoginService loginService = (ILoginService)ctx.getBean("loginService");
- loginService.login("zhangsan", "12344");
- }
- public static void main(String[] args) {
- testSpringAOP();
- }
- }
- login:zhangsan,12344
- 目标参数列表:
- zhangsan,
- 12344,
- 执行结果是:true
解析:1.先调用了login()方法System.out.println("login:" + userName + "," + password);
2.再调用了logArgAndReturn()方法输出了日志,并且返回了login()方法是否成功
- System.out.println("目标参数列表:");
- if (args != null) {
- for (Object obj : args) {
- System.out.println(obj + ",");
- }
- System.out.println();
- System.out.println("执行结果是:" + returnObj);
- }
Spring AOP进行日志记录,管理的更多相关文章
- Spring AOP进行日志记录
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- Spring AOP 完成日志记录
Spring AOP 完成日志记录 http://hotstrong.iteye.com/blog/1330046
- Spring AOP的日志记录
现在的项目是Spring+MyBatis,前段时间项目经理让我干了一个活,就是给所有的controller里的所有方法加上日志记录的代码,其实没有多少,也就300来个方法,也没有抱怨什么,一边打着瞌睡 ...
- 自定义注解-aop实现日志记录
关于注解,平时接触的可不少,像是 @Controller.@Service.@Autowried 等等,不知道你是否有过这种疑惑,使用 @Service 注解的类成为我们的业务类,使用 @Contro ...
- Spring Boot 之日志记录
Spring Boot 之日志记录 Spring Boot 支持集成 Java 世界主流的日志库. 如果对于 Java 日志库不熟悉,可以参考:细说 Java 主流日志工具库 关键词: log4j, ...
- Spring aop与HibernateTemplate——session管理(每事务一次 Session)
一.HibernateTemplate与Spring aop简介 参见http://bbs.csdn.net/topics/340207475中网友blueram的发言.(感谢blueram) 二.在 ...
- spring boot集成aop实现日志记录
1.pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring AOP的实现记录操作日志
适用场景: 记录接口方法的执行情况,记录相关状态到日志中. 注解类:LogTag.java package com.lichmama.spring.annotation; import java.la ...
- 使用Spring AOP 实现日志管理(简单教程)
有时候,我们在做项目时会遇到这样的需求: 给XXX.java中的所有方法加上指定格式的日志输出. 针对这种指定类.或者指定方法进行共性操作的功能,我们完全可以使用Spring AOP来实现. 本文使用 ...
随机推荐
- android图片加水印,文字
两种方法: 1.直接在图片上写文字 String str = "PICC要写的文字"; ImageView image = (ImageView) this.findViewByI ...
- socket常见几种异常
第1个异常是 java.net.BindException:Address already in use: JVM_Bind. 该异常发生在服务器端进行newServerSocket(port)(po ...
- HDU 1814 Peaceful Commission
2-SAT,输出字典序最小的解,白书模板. //TwoSAT输出字典序最小的解的模板 //注意:0,1是一组,1,2是一组..... #include<cstdio> #include&l ...
- Zabbix之配置文件详解
zabbix的配置文件一般有三种:zabbixserver的配置文件zabbix_server.confzabbixproxy的配置文件zabbix_proxy.confzabbix_agentd的配 ...
- iOS数据持久化之数据库:SQLite和FMDB
SQLite: SQLite是一款轻量级型的数据库,资源占用少.性能良好和零管理成本,具有零配置(无需安装和管理配置).独立(没有额外依赖).储存在单一磁盘文件中的一个完整的数据库.源码完全的开源.比 ...
- 玩Mega8 智能充电器-12. 终于实现-dV检测(转)
源:http://blog.chinaunix.net/uid-10701701-id-91873.html 2010.1.3 5:30终于补齐了. 电池充电的-dv 的检测系列图片请移步: http ...
- vim的复制粘贴小结(转)
原文地址:http://lsong17.spaces.live.com/blog/cns!556C21919D77FB59!603.entry 内容: 用vim这么久 了,始终也不知道怎么在vim中使 ...
- USACO Section 1.2 Milking Cows 解题报告
题目 题目描述 有3个农夫每天早上五点钟便起床去挤牛奶,现在第一个农夫挤牛奶的时刻为300(五点钟之后的第300个分钟开始),1000的时候结束.第二个农夫从700开始,1200结束.最后一个农夫从1 ...
- linux nfs文件夹、文件共享
◆一.概念 NFS是网络文件系统(Network File System)的简称,是分布式计算机系统的一个组成部分,可实现在异构网络上共享和装配远程文件系统. NFS由SUN公司开发,目前已成为文件服 ...
- 关于css的伪类和伪元素
现在才发现自己一直没有分清楚css的伪类和伪元素啊,so,总结一下. CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪元素用于将特殊的效果添加到某些选择器. 可以明确两点,第一两者都与选择器相 ...