作为运营除了处理系统生产问题,还要处理大量的用户上报事件,这部分工作占用了大量的人力。所有考虑把一部分事件查询处理做成一个自助平台,让用户自行核查处理。于是就有了用户自助系统。考虑到如何计量这个工具平台具体的实现价值,需要做用户操作统计才能给出可信服的数据。
以上就是本文的背景。自助系统的架构就是传统的springmvc+spinrg+mybatis+oracle。想到日志记录首先想到的就是AOP拦截处理。网上相关的技术贴很多。简单的小项目遇到的问题一般度娘都能给解决了~\(≧▽≦)/~
自定义注解:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)public @interface LogAnnotation { String operateModelName() default ""; String operateFuncName() default ""; String operationType() default ""; String operateDescribe() default ""; }
在controller层引用注解
@Controller @RequestMapping(value="/kit")public class QueryClientInfoController { @Resourceprivate IClientService clientService; private Logger logger = Logger.getLogger(this.getClass().getName()); @RequestMapping(value="/queryClientRegInfo") @LogAnnotation(operateModelName="用户注册模块", operateFuncName="queryClientRegInfo", operationType="query", operateDescribe="查询客户注册信息")public void queryClientRegInfo(HttpServletRequest request,HttpServletResponse response){ String client_idno = request.getParameter("idno");Client client = clientService.queryClientRegInfo(client_idno);//调用service System.out.println("queryClientRegInfo:"+client.getName()); String jsonMap = JSON.toJSONString(client); response.setCharacterEncoding("UTF-8");try { response.getWriter().write(jsonMap); response.getWriter().flush(); response.getWriter().close(); } catch (IOException e) { e.printStackTrace(); } } }
查询用户注册信息相关其他的service层等具体实现代码就不贴出来了。
拦截器的实现
@Service(value="userLogService")public class UserLogServiceImpl implements IUserLogService { @Resourceprivate UserLogDao userLogDao; @Overridepublic void insert(UserLog record) {return userLogDao.insert(record); }@Overridepublic void logBusiController(JoinPoint joinPoint) { Map<String, Object> map = new HashMap<String, Object>(); String targetName = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); Object[] arguments = joinPoint.getArgs(); Class targetClass;try { targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods();for (Method method : methods) {if(method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes();if(clazzs.length == arguments.length ) {if(method.getAnnotation(LogAnnotation.class)!= null){ map.put("operateModelName", method.getAnnotation(LogAnnotation.class).operateModelName()); map.put("operateFuncName", method.getAnnotation(LogAnnotation.class).operateFuncName()); map.put("operationType", method.getAnnotation(LogAnnotation.class).operationType()); map.put("operateDescribe", method.getAnnotation(LogAnnotation.class).operateDescribe()); }elsebreak; } } } } catch (ClassNotFoundException e) { e.printStackTrace(); } HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpSession session =request.getSession(); User user = (User) session.getAttribute("currentUser"); System.out.println("currentUser:"+user.getName()+"\n targetName:"+targetName+"\n methodName:"+methodName+"\n operateModelName:"+map.get("operateModelName")+"\n operateFuncName:"+map.get("operateFuncName")+"\n operationType:"+map.get("operationType")+"\n operateDescribe:"+map.get("operateDescribe"));} }
接下来需要再配置文件中做拦截设置:
在spring-mvc.xml中增加
<bean id="LogService" class="com.bbc_kit.common.service.impl.UserLogServiceImpl"></bean> <aop:config> <aop:pointcut id="logBusiControllerPoint" expression="execution(* com.bbc_kit.business.controller..*.*(..))" /> <aop:aspect id="logService" ref="LogService"> <aop:after pointcut-ref="logBusiControllerPoint" method="logBusiController"/> </aop:aspect> </aop:config>
我的controller原来就是在mvc配置中做的扫描注入,service是在applicationContext.xml中做的扫描注入。所有要拦截service层的操作,可以把上面的配置微调下放置在applicationContext.xml中。
调用该.do后后台日志输出
currentUser:renhuang targetName:com.bbc_kit.business.controller.QueryClientInfoController methodName:queryClientRegInfo operateModelName:用户注册模块 operateFuncName:queryClientRegInfo operationType:query operateDescribe:查询客户注册信息
Atas ialah kandungan terperinci 用户操作拦截并作日志记录--自定义注解+AOP拦截技术讲解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!