[置顶] 使用sping AOP 操作日志管理
记录后台操作人员的登陆、退出、进入了哪个界面、增加、删除、修改等操作
在数据库中建立一张SYSLOG表,使用Sping 的AOP实现日志管理,在Sping.xml中配置
<!-- Spring 获取配置文件中定义的bean -->
<aop:aspectj-autoproxy proxy-target-class="true"/><!-- 开启切面编程功能 -->
<context:component-scan base-package="com.geeboo.wxbus.service.impl,com.geeboo.wxbus.interceptor"/>
<context:annotation-config/>
然后在interceptor包下创建一个MyInterceptor类,里面有anyMethod ()。doBefore(),doAfterReturning(),doAfterThrowing(),
doAfter(),doAround()等方法。
在doAround()中执行判断用户进行进入那个类并且那个方法,然后添加日志记录。
// 调用方法名称
String methodName =pjp.getSignature().getName();
//获取进入的类名
StringclassName=
pjp.getSignature().getDeclaringTypeName();
className =
className.substring(className.lastIndexOf(".") 1).trim();
package com.geeboo.wxbus.interceptor; import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.regex.Pattern; import net.sf.json.JSONObject; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.jeecgframework.core.util.ContextHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.geeboo.wxbus.entity.FeedbackInfo; import com.geeboo.wxbus.entity.SnackCollect;
import com.geeboo.wxbus.entity.SysLog;
import com.geeboo.wxbus.entity.Tuser;
import com.geeboo.wxbus.entity.UserSuggest;
import com.geeboo.wxbus.pageModel.FeedbackInfoPage;
import com.geeboo.wxbus.pageModel.SnackCollectPage;
import com.geeboo.wxbus.pageModel.User;
import com.geeboo.wxbus.pageModel.UserSuggestPage;
import com.geeboo.wxbus.service.FeedbackInfoServiceI;
import com.geeboo.wxbus.service.SnackCollectServiceI;
import com.geeboo.wxbus.service.SysLogServiceI;
import com.geeboo.wxbus.service.UserServiceI;
import com.geeboo.wxbus.service.UserSuggestServiceI; @Aspect
@Component
public class MyInterceptor { private SysLogServiceI logServiceI;//日志
private UserServiceI userService;//用户
private FeedbackInfoServiceI feedbackInfoService;//反馈
private SnackCollectServiceI snackCollectService;//商店
private UserSuggestServiceI userSuggestService;//用户建议 public UserServiceI getUserService() {
return userService;
}
@Autowired
public void setUserService(UserServiceI userService) {
this.userService = userService;
}
public FeedbackInfoServiceI getFeedbackInfoService() {
return feedbackInfoService;
}
@Autowired
public void setFeedbackInfoService(FeedbackInfoServiceI feedbackInfoService) {
this.feedbackInfoService = feedbackInfoService;
}
public SnackCollectServiceI getSnackCollectService() {
return snackCollectService;
}
@Autowired
public void setSnackCollectService(SnackCollectServiceI snackCollectService) {
this.snackCollectService = snackCollectService;
}
public UserSuggestServiceI getUserSuggestService() {
return userSuggestService;
}
@Autowired
public void setUserSuggestService(UserSuggestServiceI userSuggestService) {
this.userSuggestService = userSuggestService;
}
public SysLogServiceI getLogServiceI() {
return logServiceI;
}
@Autowired
public void setLogServiceI(SysLogServiceI logServiceI) {
this.logServiceI = logServiceI;
} /**
* 第一个* 代表任意的返回类型 (..) 所有参数
*/
@Pointcut("execution(* com.geeboo.wxbus.service.impl.*.*(..))")
private void anyMethod() {
}; // 声明一个切入点 @Before("anyMethod() && args(object)")
public void doBefore(Object object) {
//System.out.println("前置通知" + object);
} @AfterReturning(pointcut = "anyMethod()", returning = "name")
public void doAfterReturning(String name) {
//System.out.println("后置通知:" + name);
} @AfterThrowing("anyMethod()")
public void doAfterThrowing() {
//System.out.println("例外通知");
} @After("anyMethod() && args(object)" )
public void doAfter(Object object) {
//System.out.println("最终通知"+object);
} @Around("anyMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable { // 要执行pip.proceed方法 // 调用方法名称
String methodName = pjp.getSignature().getName();
//获取进入的类名
String className = pjp.getSignature().getDeclaringTypeName();
className = className.substring(className.lastIndexOf(".") + 1).trim(); if(className.equals("SysLogServiceImpl")||className.equals("TotalMsgServiceImpl")||className.equals("TotalUserServiceImpl")||className.equals("MessageServiceImpl")){ //如果是日志的就不用
return pjp.proceed();
}
// 调用参数
Object[] args = pjp.getArgs();
Object object = null;
// System.out.println("==============进去的方法"+methodName);
if(Pattern.matches("(add|update|delete)[\\S]*",
methodName)) { String logMsg="";//日志消息内容 Tuser user=(Tuser) ContextHolderUtils.getSession().getAttribute("USER_SESSION");//获取用户名
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=sdf.format(new Date());//获取当前时间 //System.out.println("进入的类名"+className); JSONObject msg = new JSONObject();
for (Object temp : args) {
Class<? extends Object> paramClazz = temp.getClass();
String classType = paramClazz.getName();
if (classType.equals("java.lang.String")) {
msg.put("key", temp);
} else if (classType.equals("java.util.HashMap")) {
msg.putAll((HashMap<?, ?>) temp);
} else if (classType.startsWith("com.")) {
try {
Field[] f = paramClazz.getDeclaredFields();
for (Field field : f) {
String fieldName = field.getName();
field.setAccessible(true);
msg.put(fieldName, field.get(temp));
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
//用户管理
if(className.equals("UserServiceImpl")){
if(user!=null){
logMsg="用户名:"+user.getCname()+"-在-"+"操作用户";
}else{
logMsg="";
}
JSONObject msg1 = new JSONObject();//保存在JSON中然后添加进去
if(methodName.equals("update")){ //修改之前获取原来的数据
User u=new User();
u.setCid(msg.getString("cid").toString());//获取删除的主键
Tuser lastUser=userService.get(u);
msg1.put("user", lastUser);
logMsg=logMsg+"-进入修改!修改前数据为:"+msg1+"****************修改后数据为:"+msg;
}else if(methodName.equals("delete")){
User u=new User();
u.setCid(msg.getString("key").toString());//获取删除的主键
Tuser lastUser=userService.get(u);
msg1.put("user", lastUser);
logMsg=logMsg+"-进入删除操作值为:"+msg1;
} }
//反馈管理
else if(className.equals("FeedbackInfoServiceImpl")){
logMsg=user.getCname()+"-在-"+"操作反馈信息";
JSONObject msg1 = new JSONObject();//保存在JSON中然后添加进去
if(methodName.equals("update")){ //进入修改界面
FeedbackInfoPage f=new FeedbackInfoPage();
//System.out.println("反馈信息"+msg);
f.setFeedbackId(msg.getString("feedbackId"));
FeedbackInfo lastFeebackInfo=feedbackInfoService.get(f);
msg1.put("feebackInfo", lastFeebackInfo);
}else if(methodName.equals("delete")){
FeedbackInfoPage f=new FeedbackInfoPage();
//System.out.println("反馈信息"+msg);
f.setFeedbackId(msg.getString("key"));
FeedbackInfo lastFeebackInfo=feedbackInfoService.get(f);
msg1.put("feebackInfo", lastFeebackInfo);
}
if(methodName.equals("update")){
logMsg=logMsg+"-进入修改!修改前数据为:"+msg1+"****************修改后数据为:"+msg;
}else if(methodName.equals("delete")){
logMsg=logMsg+"-进入删除操作值为:"+msg1;
}
}
//用户建议管理
else if(className.equals("UserSuggestServiceImpl")){
logMsg=user.getCname()+"-在-"+"操作用户建议";
JSONObject msg1 = new JSONObject();//保存在JSON中然后添加进去
if(methodName.equals("delete")){
UserSuggestPage suggest=new UserSuggestPage();
//System.out.println("用户建议"+msg);
suggest.setSuggestId(msg.getString("key"));
UserSuggest lastSuggest=userSuggestService.get(suggest);
msg1.put("suggest", lastSuggest);
logMsg=logMsg+"-进入删除操作值为:"+msg1;
} }
//店家管理
else if(className.equals("SnackCollectServiceImpl")){
logMsg=user.getCname()+"-在-"+"操作商店";
JSONObject msg1 = new JSONObject();//保存在JSON中然后添加进去
if(methodName.equals("update")){
SnackCollectPage snackCollect=new SnackCollectPage();
//System.out.println("店家管理"+msg);
snackCollect.setSnackCollectId(msg.getString("snackCollectId"));
SnackCollect lastSnack=snackCollectService.get(snackCollect);
msg1.put("snack", lastSnack);
logMsg=logMsg+"-进入修改!修改前数据为:"+msg1+"****************修改后数据为:"+msg;
//System.out.println("店家管理修改后的数据"+logMsg);
}else if(methodName.equals("delete")){
SnackCollectPage snackCollect=new SnackCollectPage();
//System.out.println("店家管理"+msg);
snackCollect.setSnackCollectId(msg.getString("key"));
SnackCollect lastSnack=snackCollectService.get(snackCollect);
msg1.put("snack", lastSnack);
logMsg=logMsg+"-进入删除操作值为:"+msg1;
}
}
if(methodName.equals("add")){ //进入获取添加方法
logMsg=logMsg+"-进入添加操作值为:"+msg;
} SysLog log=new SysLog();
log.setLogId(UUID.randomUUID().toString());
log.setLogMsg(logMsg);
log.setLogTime(time);
logServiceI.add(log);
}
return pjp.proceed();
} }
[置顶] 使用sping AOP 操作日志管理的更多相关文章
- Spring Boot 入门(五):集成 AOP 进行日志管理
本篇文章是接着 Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理写的,按照前面几篇博客的教程,可以搭建一个简单的项目,主要包含了 Pagehelper+MyBatis 分页 ...
- 【Java分享客栈】超简洁SpringBoot使用AOP统一日志管理-纯干货干到便秘
前言 请问今天您便秘了吗?程序员坐久了真的会便秘哦,如果偶然点进了这篇小干货,就麻烦您喝杯水然后去趟厕所一边用左手托起对准嘘嘘,一边用右手滑动手机看完本篇吧. 实现 本篇AOP统一日志管理写法来源于国 ...
- Spring Boot AOP 简易操作日志管理
AOP (Aspect Oriented Programming) 面向切面编程. 业务有核心业务和边缘业务. 比如用户管理,菜单管理,权限管理,这些都属于核心业务. 比如日志管理,操作记录管理,这些 ...
- 【Java EE 学习 76 下】【数据采集系统第八天】【通过AOP实现日志管理】【日志管理功能分析和初步实现】
一.日志管理相关分析 1.日志管理是一种典型的系统级别的应用,非常适合使用spring AOP实现. 2.使用日志管理的目的:对系统修改的动作进行记录,比如对权限.角色.用户的写操作.修改操作.删除操 ...
- .NetCore中使用AspectCore、ExceptionLess 实现AOP操作日志记录
结合前面封装的ExceptionLess,接下来使用 AspectCore 实现AOP日志处理 nuget导入AspectCore.Core .AspectCore.Extensions.Depend ...
- 【Java EE 学习 77 上】【数据采集系统第九天】【通过AOP实现日志管理】【通过Spring石英调度动态生成日志表】【日志分表和查询】
一.需求分析 日志数据在很多行业中都是非常敏感的数据,它们不能删除只能保存和查看,这样日志表就会越来越大,我们不可能永远让它无限制的增长下去,必须采取一种手段将数据分散开来.假设现在整个数据库需要保存 ...
- 通过aop添加日志管理
1.使用spring 的 aop 技术切到自定义注解上,所以先创建一个自定义注解类 import java.lang.annotation.*; @Target(ElementType.METHOD) ...
- 使用Spring AOP 实现日志管理(简单教程)
有时候,我们在做项目时会遇到这样的需求: 给XXX.java中的所有方法加上指定格式的日志输出. 针对这种指定类.或者指定方法进行共性操作的功能,我们完全可以使用Spring AOP来实现. 本文使用 ...
- [置顶] Jquery中DOM操作(详细)
Jquery中的DOM操作 为了能全面的讲解DOM操作,首先需要构建一个网页. HTML代码: <%@ page language="java" import="j ...
随机推荐
- ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则
原文:ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则 对于Model验证,理想的设计应该是场景驱动的,而不是Model(类型)驱动的,也就是对于同一个Model对象, ...
- Error with mysqld_safe
出处:http://bugs.mysql.com/bug.php?id=18403 Description: - I downloaded the binary file “Standard 5.0. ...
- UVA Graph Coloring
主题如以下: Graph Coloring You are to write a program that tries to find an optimal coloring for agiven ...
- leetcode第21题--Generate Parentheses
problem: Given n pairs of parentheses, write a function to generate all combinations of well-formed ...
- [翻译]初识SQL Server 2005 Reporting Services Part 3
原文:[翻译]初识SQL Server 2005 Reporting Services Part 3 这是关于SSRS文章中四部分的第三部分.Part 1提供了一个创建基本报表的递阶教程.Part 2 ...
- 最佳新秀Java(22)——再次了解泛型
仿制药Java SE 1.5新功能.通用自然是参数化类型.即操作数据类型被指定为一个参数.这样的参数类型可以在课堂上使用.创建的接口和方法,他们被称为通用类..泛型方法. Java语言引入泛型的优点是 ...
- ASP.NET MVC项目
ASP.NET MVC项目里创建一个aspx视图 先从控制器里添加视图 视图引擎选"ASPX(C#)",使用布局或模板页不要选. 在Views\EAV目录里,生成的aspx是个单独 ...
- iTextSharp生成pdf文档案例
1.using iTextSharp.text;using iTextSharp.text.pdf; 2.设置页面大小 iTextSharp.text.Rectangle pageSize = new ...
- MVC UnitOfWork EntityFramework架构
MVC UnitOfWork EntityFramework架构,网站速度慢的原因总结! 最近参考使用了郭明峰的一套架构来做新的项目架构,这套架构看起来还是不错的,先向小郭同学的分享精神致敬! (郭同 ...
- github上写个人简历
随笔- 70 文章- 0 评论- 567 在github上写个人简历——先弄个主页 起因 不知道园友们在使用智联招聘等网站填写简历的时候对要求输入的内容有没有一种无力感,不吐槽了反正就一句话 ...