spring_aop_日志配置.docx_第1页
spring_aop_日志配置.docx_第2页
spring_aop_日志配置.docx_第3页
spring_aop_日志配置.docx_第4页
spring_aop_日志配置.docx_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 首先引入jar包 spring.jar aopalliance.jar aspectjrt.jar aspectjtools.jar aspectjweaver.jar cglib-nodep-2.1_3.jar cglib-src-2.2.jar 这些个jar包不一定都用得上,但离配置到现在整理已经比较久了 有点忘了.2 spring 配置文件头 3 定义 aop拦截 4 处理拦截的类logMonitorAspectpublic class LogMonitor /* * 保存变量的ThreadLocal,保持在同一线程中同步数据. */private static final ThreadLocal SESSION_MAP = new ThreadLocal();private IUserLogInfoService userLogInfoService;public IUserLogInfoService getUserLogInfoService() return userLogInfoService;public void setUserLogInfoService(IUserLogInfoService userLogInfoService) this.userLogInfoService = userLogInfoService;/* * 获得线程中保存的属性. * * param attribute * 属性名称 * return 属性值 */public static Object get(String attribute) Map map = (Map) SESSION_MAP.get();return map.get(attribute);/* * 记录日志信息 * param joinpoint * throws Exception * throws IllegalAccessException */public void writeLogInfo(JoinPoint joinpoint) throws Exception, IllegalAccessException UserInfoVO sysUse = (UserInfoVO) UserSession.get(Contents.SESSION_USER_KEY);/ 在基类action中 设用户信息/UserSession.set(Contents.SESSION_USER_KEY, userVO);/UserSession.set(Contents.SESSION_USER_IP, request.getRemoteAddr();String userIP = (String) UserSession.get(Contents.SESSION_USER_IP);String temp = joinpoint.getStaticPart().toShortString();/ 获得操作类String classType = joinpoint.getTarget().getClass().getName();/ 获得方法名称String methodName = temp.substring(10, temp.length() - 1);Class className = Class.forName(classType);/ 获得方法的参数类型数组Object a = joinpoint.getArgs();Class o = new Classa.length;/ for (int i = 0; i a.length; i+) oi = ai.getClass();Method method = className.getMethod(methodName, o);LogDesc desc = method.getAnnotation(LogDesc.class);/ 获得方法的操作动作类型说明String actionDesc = ;if (desc != null) actionDesc = desc.actionDesc();/ 记录日志信息UserLogInfo userLogInfo = new UserLogInfo();if (sysUse != null) userLogInfo.setUserID(sysUse.getUserID();userLogInfo.setUserIp(userIP);userLogInfo.setOperateClass(classType);userLogInfo.setOperateMethod(methodName);/设置为一般日志信息标识位userLogInfo.setEnableFlag(C);userLogInfo.setOperateDescribe(actionDesc);userLogInfoService.addUserLog(userLogInfo);5 定义取得拦截方法描述的类 Retention(RetentionPolicy.RUNTIME)Target(ElementType.METHOD)public interface LogDesc String actionDesc();6 在每个需要拦截的方法头加上/* * 注册新用户 * * roseuid 3C30BD930399 */LogDesc(actionDesc = 新建会员)public MemberVO createMember(MemberVO memberVO) throws SysComException return memberRegisterDao.createMember(memberVO);7 设置、取得用户信息public class UserSession/private static ThreadLocal threadLocal = new ThreadLocal(); /public HttpServletRequest getContext() /return (HttpServletRequest)threadLocal.get(); / /public void setContext(HttpServletRequest request) /threadLocal.set(request); / /public void cleanContext() /threadLocal.set(null); / /* * 保存变量的ThreadLocal,保持在同一线程中同步数据. */ private static final ThreadLocal SESSION_MAP = new ThreadLocal(); /* * 工具类的protected构造方法. */ protected UserSession() /* * 获得线程中保存的属性. * * param attribute * 属性名称 * return 属性值 */ public static Object get(String attribute) Map map = (Map) SESSION_MAP.get(); /System.out.println(map.toString(); / System.out.println(map.containsKey(usersession); return map.get(attribute); /* * 获得线程中保存的属性,使用指定类型进行转型. * * param attribute * 属性名称 * param clazz * 类型 * param * 自动转型 * return 属性值 */ public static T get(String attribute, Class clazz) return (T) get(attribute); /* * 设置制定属性名的值. * * param attribute * 属性名称 * param value * 属性值 */ public static void set(String attribute, Obj

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论