作為營運除了處理系統生產問題,還要處理大量的使用者回報事件,這部分工作佔用了大量的人力。所有考慮把一部分事件查詢處理做成自助平台,讓使用者自行查證處理。於是就有了用戶自助系統。考慮到如何計量這個工具平台具體的實現價值,需要做使用者操作統計才能給出可信任服的資料。
以上就是本文的背景。自助系統的架構就是傳統的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:查询客户注册信息
以上是使用者操作攔截並作日誌記錄--自訂註解+AOP攔截技術講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!