【JAVA笔记——器】Spring Aop 实现Log日志系统——基本实现
2016-09-07 23:10
501 查看
Log日志系统可以说是项目开发中最基本的模块之一,在未使用Spring Aop之前,日志记录都是采用手工配置。由于开发人员的代码风格不统一,经常会导致日志风格混乱,难以阅读,甚至日志遗漏情况。
通过Aop可以实现日志系统的自动配置,减少人工配置的错误风险,同时提高日志系统的健壮性。
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:task=”http://www.springframework.org/schema/task”
通过Aop可以实现日志系统的自动配置,减少人工配置的错误风险,同时提高日志系统的健壮性。
基本配置
xmlns:context=”http://www.springframework.org/schema/context”xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:task=”http://www.springframework.org/schema/task”
<!-- 容器配置 --> <context:component-scan base-package="com.cunchen.aop.common"/> <!-- 上下文 注解 --> <context:annotation-config/> <!-- 注解自动代理 --> <aop:aspectj-autoproxy/>
代码实现
aop配置不明白的地方,可以参照【JAVA笔记——器】Spring面向切面编程 Aspect Oriented Programming with Spring/** * 日志切面 * @author cunchen * 2016年9月7日下午11:03:08 */ @Aspect @Component public class LogAspect { /** * 环绕通知 * 匹配所有com.cunchen.*.service service方法且只有一个参数 * @param pjp * @param param * @return * @throws Throwable */ @Around("execution(* com.cunchen.*.service.*.*(..)) && args(param)") public Object doBasicProfiling(ProceedingJoinPoint pjp, Object param) throws Throwable { LogFileWriter writer = new LogFileWriter(); writer.outputLog(pjp.getSignature().toLongString() + "\t time = " + System.currentTimeMillis()); Object retVal = pjp.proceed(new Object[]{param}); if(retVal != null) writer.outputLog("result = " + retVal.toString() + "\t time = " + System.currentTimeMillis()); else writer.outputLog("spend time = " + System.currentTimeMillis()); return retVal; } } ** * Log文件输出 * @author cunchen * 2016年9月7日下午2:40:26 */ public class LogFileWriter { public boolean outputLog(String log) { URL url = ClassLoader.getSystemResource(""); System.out.println("\nurl = " + url + "\n"); File f = new File(url.getFile() + "test.log"); BufferedWriter writer = null; try { if(!f.isFile()) { f.getParentFile().mkdirs(); f.createNewFile(); } else { writer = new BufferedWriter(new FileWriter(f, true)); writer.append(log + "\n"); writer.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { if(writer != null) { try { writer.flush(); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return false; } }
相关文章推荐
- Spring AOP实现系统日志记录
- 请教.Net平台大型业务系统中“日志记录” 的较好解决办法,类似Java下用AOP方式实现的最好
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- 利用Spring AOP与JAVA注解为系统增加日志功能
- 从零开始学 Java - Spring AOP 拦截器的基本实现
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring AOP 实现系统操作日志记录
- springMVC +Mybatis +spring aop 实现用户系统操作日志记录
- Spring AOP 实现系统操作日志记录
- Spring AOP自定义注解实现系统日志记录管理
- 2017/2/23 Java SpringAOP拦截Service实现日志管理(自定义注解)
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- java学习笔记10 - 以@AspectJ方式在Spring中实现AOP
- Spring -aop 实现系统日志
- Spring中应用java动态代码实现Aop
- 基于james3.0 的邮件系统(struts2.3.2 +spring3.0.1+jpa(hibernate3.6.5)实现)b/s模式--java邮件系统
- Spring学习笔记(14)----使用Spring的注解方式实现AOP