2019独角兽企业重金招聘Python工程师标准>>>
第一步定义两个注解
package com.annotation; import java.lang.annotation.*; /** *自定义注解 拦截Controller */ Target({ElementType.PARAMETER, ElementType.METHOD}) Retention(RetentionPolicy.RUNTIME) Documented public interface SystemControllerLog { String description() default ""; } package com.annotation; import java.lang.annotation.*; /** *自定义注解 拦截service */ Target({ElementType.PARAMETER, ElementType.METHOD}) Retention(RetentionPolicy.RUNTIME) Documented public interface SystemServiceLog { String description() default ""; }
第二步创建一个切点类
package com.annotation; import com.model.Log; import com.model.User; import com.service.LogService; import com.util.DateUtil; import com.util.JSONUtil; import com.util.SpringContextHolder; import com.util.WebConstants; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.lang.reflect.Method; /** * 切点类 * author tiangai * since 2014-08-05 Pm 20:35 * version 1.0 */ Aspect Component public class SystemLogAspect { //注入Service用于把日志保存数据库 Resource private LogService logService; //本地异常日志记录对象 private static final Logger logger LoggerFactory.getLogger(SystemLogAspect.class); //Service层切点 Pointcut("annotation(com.annotation.SystemServiceLog)") public void serviceAspect() { } //Controller层切点 Pointcut("annotation(com.annotation.SystemControllerLog)") public void controllerAspect() { } /** * 前置通知 用于拦截Controller层记录用户的操作 * * param joinPoint 切点 */ Before("controllerAspect()") public void doBefore(JoinPoint joinPoint) { HttpServletRequest request ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpSession session request.getSession(); //读取session中的用户 User user (User) session.getAttribute(WebConstants.CURRENT_USER); //请求的IP String ip request.getRemoteAddr(); try { //*控制台输出*// System.out.println("前置通知开始"); System.out.println("请求方法:" (joinPoint.getTarget().getClass().getName() "." joinPoint.getSignature().getName() "()")); System.out.println("方法描述:" getControllerMethodDescription(joinPoint)); System.out.println("请求人:" user.getName()); System.out.println("请求IP:" ip); //*数据库日志*// Log log SpringContextHolder.getBean("logxx"); log.setDescription(getControllerMethodDescription(joinPoint)); log.setMethod((joinPoint.getTarget().getClass().getName() "." joinPoint.getSignature().getName() "()")); log.setType("0"); log.setRequestIp(ip); log.setExceptionCode(null); log.setExceptionDetail(null); log.setParams(null); log.setCreateBy(user); log.setCreateDate(DateUtil.getCurrentDate()); //保存数据库 logService.add(log); System.out.println("前置通知结束"); } catch (Exception e) { //记录本地异常日志 logger.error("前置通知异常"); logger.error("异常信息:{}", e.getMessage()); } } /** * 异常通知 用于拦截service层记录异常日志 * * param joinPoint * param e */ AfterThrowing(pointcut "serviceAspect()", throwing "e") public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { HttpServletRequest request ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpSession session request.getSession(); //读取session中的用户 User user (User) session.getAttribute(WebConstants.CURRENT_USER); //获取请求ip String ip request.getRemoteAddr(); //获取用户请求方法的参数并序列化为JSON格式字符串 String params ""; if (joinPoint.getArgs() ! null 0; i
在实例化ApplicationContext的时候需要加上
在调用Controller的时候AOP发挥作用所以在SpringMVC的配置文件里加上
Controller层的使用
/*** 删除用户 * * param criteria 条件 * param id id * param model 模型 * return 数据列表 */RequestMapping(value "/delete") //此处为记录AOP拦截Controller记录用户操作 SystemControllerLog(description "删除用户") public String del(Criteria criteria, String id, Model model, HttpSession session) { try { User user (User) session.getAttribute(WebConstants.CURRENT_USER); if (null ! user) { if (user.getId().equals(id)) { msg "您不可以删除自己!"; criteria userService.selectByCriteriaPagination(criteria); } else { //删除数据并查询出数据 criteria userService.delete(id, criteria); msg "删除成功!"; } } } catch (Exception e) { msg "删除失败!"; } finally { model.addAttribute("msg", msg); model.addAttribute("criteria", criteria); } //跳转列表页 return "user/list"; }
Service层的使用
/*** 按照分页查询 * param criteria * return *///此处为AOP拦截Service记录异常信息。方法不需要加try-catch SystemServiceLog(description "查询用户") public Criteria selectByCriteriaPagination(Criteria criteria) { criteria.getList().get(0).getAccount(); //查询总数 long totaluserMapper.countByCriteria(criteria); //设置总数 criteria.setRowCount(total); criteria.setList(userMapper.selectByCriteriaPagination(criteria)); return criteria; }
转:https://my.oschina.net/u/2356710/blog/895737