User operation interception and logging-custom annotation + AOP interception, logging aop
As an operator, in addition to handling system production problems, it also needs to handle a large number of user reporting events, which occupy a lot of manpower. All users are considering making part of Event Query and processing a self-help platform, allowing users to verify and handle the event on their own. So we have a user self-service system. Considering how to measure the specific implementation value of this tool platform, we need to make user operation statistics to provide convincing data.
The above is the background of this article. The self-help system architecture is the traditional springmvc + spinrg + mybatis + oracle. The first thought of logging is the AOP interception processing. There are many technical posts on the Internet. The problems encountered in simple small projects are generally solved by Du Niang ~ \ (Too many rows )/~
Custom annotation:
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface LogAnnotation { String operateModelName() default ""; String operateFuncName() default ""; String operationType() default ""; String operateDescribe() default "";}
Reference annotation at the controller Layer
@ Controller @ RequestMapping (value = "/kit") public class QueryClientInfoController {@ Resource private IClientService clientService; private Logger logger = Logger. getLogger (this. getClass (). getName (); @ RequestMapping (value = "/queryClientRegInfo") @ LogAnnotation (operateModelName = "User Registration Module", operateFuncName = "queryClientRegInfo", operationType = "query ", operateDescribe = "querying customer registration information") public void queryClientRegInfo (HttpServletRequest request, HttpServletResponse response) {String client_idno = request. getParameter ("idno"); Client client = clientService. queryClientRegInfo (client_idno); // call 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 ();}}}
The specific implementation code for querying other service layers related to user registration information will not be pasted out.
Interceptor implementation
@Service(value="userLogService")public class UserLogServiceImpl implements IUserLogService { @Resource private UserLogDao userLogDao; @Override public void insert(UserLog record) { return userLogDao.insert(record); } @Override public 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()); }else break; } } } } 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")); }}
Next, configure the interception settings in the configuration file:
Add in 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>
My controller was originally used for scanning injection in the mvc configuration, and the service was used for scanning injection in applicationContext. xml. For all operations to intercept the service layer, You can fine-tune the above configuration and place it in applicationContext. xml.
After this. do is called, logs are output in the background.
CurrentUser: renhuang
TargetName: com. bbc_kit.business.controller.QueryClientInfoController
MethodName: queryClientRegInfo
OperateModelName: User Registration Module
OperateFuncName: queryClientRegInfo
OperationType: query
OperateDescribe: Query customer Registration Information