基于xml配置的spring aop 的实现日志 和为通知传递参数
来源:互联网 发布:马飞达车牌识别软件 编辑:程序博客网 时间:2024/06/16 13:50
1、首先是在spring配置文件下配置如下信息:
<!-- 切面 --> <bean id="myAspectOfLoginAndLoginout" class="bea.com.aop.Log_loginAndLogout"></bean> <bean id="myAspectOfAUD" class="bea.com.aop.Log_Record" > <property name="userLogService"> <ref bean="userLogService" /> </property> </bean> <!-- 切入点 --> <aop:config> <aop:aspect id="log_login" ref="myAspectOfLoginAndLoginout"> <aop:after method="log_Login" pointcut="execution(* bea.com.admin.Service.*.Login*(..))" /> </aop:aspect> <aop:aspect id="log_AUD" ref="myAspectOfAUD"> <aop:after-returning method="addUserLog" returning="returnValue" pointcut="execution(* bea.com.admin.Service.*.add*(..))" /> <aop:after-returning method="addUserLog" returning="returnValue" pointcut="execution(* bea.com.Service.*.add*(..))" /> <aop:after-returning method="modifyUserLog" returning="returnValue" pointcut="execution(* bea.com.Service.*.modify*(..))" /> <aop:after-returning method="modifyUserLog" returning="returnValue" pointcut="execution(* bea.com.admin.Service.*.modify*(..))" /> </aop:aspect> </aop:config>
2、然后在aop类里实现如下代码:
import javax.servlet.http.HttpSession;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.struts2.ServletActionContext;import org.aspectj.lang.JoinPoint;import bea.com.aop.service.UserLogService;import bea.com.spojo.TuserLog;import bea.com.util.DateParse;import bea.com.util.reflectUtil;public class Log_Record{ private static final Logger logger = LoggerFactory.getLogger(Log_Record.class); private UserLogService userLogService; public UserLogService getUserLogService() { return userLogService; } public void setUserLogService(UserLogService userLogService) { this.userLogService = userLogService; } /** * 新增日志 * @param joinPoint * @param returnValue */ public void addUserLog(JoinPoint joinPoint, Object returnValue){ Object[] args = joinPoint.getArgs(); String className="";//新增的类名 boolean flag=false; int tableId=Integer.parseInt(returnValue.toString()); //joinPoint.getTarget(); //System.out.println("target:"+joinPoint.getTarget()); //target:bea.com.Service.CunWeiService@d7721e7 HttpSession session=ServletActionContext.getRequest().getSession(); int userId=Integer.parseInt(session.getAttribute("userId").toString()); System.out.println(args[0]); //bea.com.spojo.Tcunweiandzhiwei@7470f191 Class userClass = args[0].getClass(); className=userClass.getSimpleName(); String datetimeNow=DateParse.date2String(new Date()); TuserLog tuserLog=new TuserLog(); tuserLog.setOperationTime(datetimeNow); tuserLog.setOperationType(1); tuserLog.setTableName(className); tuserLog.setUserId(userId); tuserLog.setUserId(userId); tuserLog.setTableId(tableId); this.userLogService.addUserLog(tuserLog); System.out.println("returnValue"+returnValue); } /** * 记录修改的日志 * @param joinPoint */ public void modifyUserLog(JoinPoint joinPoint,Object returnValue){ //如果修改成功! if(Boolean.parseBoolean(returnValue.toString())){ Object[] args = joinPoint.getArgs(); String className="";//修改的类名 HttpSession session=ServletActionContext.getRequest().getSession(); int userId=Integer.parseInt(session.getAttribute("userId").toString()); System.out.println(args[0]); Class userClass = args[0].getClass(); reflectUtil.reflect(args[0]); className=userClass.getSimpleName(); String datetimeNow=DateParse.date2String(new Date()); TuserLog tuserLog=new TuserLog(); tuserLog.setOperationTime(datetimeNow); tuserLog.setOperationType(1); tuserLog.setTableName(className); tuserLog.setUserId(userId); tuserLog.setUserId(userId); // tuserLog.setTableId(tableId); this.userLogService.addUserLog(tuserLog); //修改不成功 }else{ return; } }}
0 0
- 基于xml配置的spring aop 的实现日志 和为通知传递参数
- Spring -- 基于XML的AOP通知配置
- spring AOP 通知参数的传递
- Spring AOP(3)基于XML配置实现的示例
- Spring AOP(3)基于XML配置实现的示例
- Spring Xml文件配置实现AOP通知
- spring aop的五种通知类型(基于XML)
- 基于XML的spring AOP配置
- Spring的AOP配置(基于xml)
- 基于XML的Spring AOP配置
- 基于Spring AOP和Groovy日志模板配置的日志记录框架的二次实现与使用案例
- Spring实现AOP方式之一:基于XML配置的Spring AOP
- spring -- aop基于xml的简单实现
- AOP-AspectJ-基于XML的实现——各种通知
- 配置xml方式实现Spring的aop
- 【Spring aop】Spring aop的XML和注解的两种配置实现
- spring框架基于注解aop的通知
- 基于AOP的日志实现!
- hadoop mapreduce模式读取sequence文件
- 快速入手ITOO导入-改进导入.xlsx格式
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- Qt5.3 for vs2013 中文乱码解决办法
- Ubuntu环境下Qt软件deb包制作
- 基于xml配置的spring aop 的实现日志 和为通知传递参数
- 陈怡暖:金市料1200破位上涨,建议逢低做多
- 使用Adt自带的工具进行Android自动化测试(二)
- C++关键字explicit
- leetcode Add Two Numbers
- Openwrt flash 空间不足的解决方法
- SQL CONCAT函数
- u-boot启动流程分析
- 高通AR增强现实多卡识别和扩展跟踪Unity