[置顶] 使用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();
- }
- }
相关推荐
spring aop做的日志管理,网上看的,没测试过。
本资源用来展示如何使用 spring aop 进行日志记录,例子里面通过aop的配置,把产生的日志存放到当前项目的根目录下,而且对方法执行过程中的参数进行了记录,对于aop如何记录日志不清楚的同学可以看看。
一、适合人群 1、具备一定Java编程基础,初级开发者 2、对springboot,mybatis,mysql有基本认识 3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 ...4、spring boot,mybatis,druid,spring aop的使用
Spring AOP 日志管理 实例LoggingThrowsAdvice.java
采用SpringAOP拦截Controller,Service实现操作日志管理,统一处理异常,登陆日志管理,是SpringAOP的应用实践。通过SpringAOP的处理,可以方便移植日志管理功能,是个不错的学习demo
spring AOP 切面日志 分层打日志
Flex整合Spring实现aop日志管理 实现记录操作的日志
swagger和spring Aop日志结合 ,swagger和spring Aop日志结合 ,swagger和spring Aop日志结合 ,
Spring AOP--日志管理,注释齐全,欢迎大家共同交流。
添加Myeclipse自带的Spring 2.5 AOP librarys 和 Spring 2.5 Core librarys 以及 junit4
元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运维提供支持 日志记录尽可能减少性能影响
可扩展性强:使用 Spring AOP 进行方法耗时监测,可以方便的对其他的切面进行扩展,例如:日志、异常处理、权限控制等切面。 4. 提高系统性能:通过对系统中的方法进行耗时监测,可以及时发现系统中的性能瓶颈,...
spring aop 拦截系统操作日志和异常日志,存储到数据库中的全过程
该压缩包中包含了一个myeclipse6.5下开发的JAVA基于spring实现的日志记载例子,该例子提供了接受切点参数,解析切点返回值,并且都打印出来了,有详细的文档介绍。
spring boot aop 统一处理日志 demo spring boot aop 统一处理日志 demo
使用SpringAOP完成日志记录功能的实例
spring aop切面拦截指定类和方法实现流程日志跟踪 一般情况下,在不侵入业务代码的情况下,需要做流程日志跟踪是比较合理的 采用springaop切面思想
利用自定义注解和spring aop和java反射机制生成用户能够读懂的日志记录。如:用户张三在2013年9月27日17:00执行了用户管理模块的用户删除功能参数为(编号:123456)各位可根据需要写入数据库或者保存到文件。
此工程为使用eclipse创建java project,使用jdk1.8,项目中包含完整的代码和jar包,导入eclipse即可运行
spring mvc利用aop日志实现对controller层的拦截