11.AOP日志
1、数据库与表结构
1.2.sql语句
CREATE TABLE sysLog( id VARCHAR2(32) default SYS_GUID() PRIMARY KEY, visitTime timestamp, username VARCHAR2(50), ip VARCHAR2(30), url VARCHAR2(50), executionTime int, method VARCHAR2(200) )
1.3.实体类
public class SysLog { private String id; private Date visitTime; private String visitTimeStr; private String username; private String ip; private String url; private Long executionTime; private String method; //==============================================// public String getVisitTimeStr() { if(visitTime !=null){ //调用自己写的工具类转换 visitTimeStr = DateUtils.date2String(visitTime,"yyyy-MM-dd HH:mm:ss"); } return visitTimeStr; } //=============================================//
2.基于AOP日志处理
6.AOP日志中信息获取
7.AOP日志查询操作流程
为了获取访问的IP
在WEB-INF目录下的
web.xml 中配置一个spring监听器Listener
<!-- 配置监听器,监听request域对象的创建和销毁的 --> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
ip地址获取
ip地址的获取我们可以通过request.getRemoteAddr()方法获取到。
在Spring中可以通过RequestContextListener来获取request或session对象。
2.3.SysLogController
@RequestMapping("/sysLog") @Controller public class SysLogController { @Autowired private ISysLogService sysLogService; @RequestMapping("/findAll.do") public ModelAndView findAll() throws Exception{ ModelAndView mv = new ModelAndView(); List<SysLog> sysLogsList = sysLogService.findAll(); mv.addObject("sysLogs",sysLogsList); mv.setViewName("syslog-list2"); return mv; } }
2.4.Service层
@Service @Transactional public class SysLogServiceImpl implements ISysLogService { @Autowired private ISysLogDao sysLogDao; @Override public void save(SysLog log) throws Exception { sysLogDao.save(log); } @Override public List<SysLog> findAll() throws Exception { return sysLogDao.findAll(); } }
2.5.Dao层
基于注解+接口的方法使用 mybatis集成spring
public interface ISysLogDao { @Insert({"insert into SYSLOG(visitTime,username,ip,url,executionTime,method)" , "values( #{visitTime},#{username},#{ip},#{url},#{executionTime},#{method})" }) public void save(SysLog log) throws Exception; //保存日志的接口方法 @Select("select * from syslog") @Results({ @Result(id=true,column="id",property="id"), @Result(column="visitTime",property="visitTime"), @Result(column="ip",property="ip"), @Result(column="url",property="url"), @Result(column="executionTime",property="executionTime"), @Result(column="method",property="method"), @Result(column="username",property="username") }) public List<SysLog> findAll() throws Exception; //查询日志的接口方法 }
Controller层的AOP切面类
前置通知 的方法内容
@Component //spring框架的注解 @Aspect public class LogAop { @Autowired private HttpServletRequest request; //访问的IP @Autowired private ISysLogService sysLogService; private Date startTime; //访问开始时间 private Class executionClass; //访问的类 private Method executionMethod; //访问的方法 //前置通知 //主要是获取开始时间,执行的类是哪一个,执行的是哪一个方法 @Before("execution(* cn.bjut.ssm.controller.*.*(..))") public void doBefore(JoinPoint jp) throws NoSuchMethodException, SecurityException{ //开始访问的时间 startTime = new Date(); //具体要访问的类 executionClass = jp.getTarget().getClass(); String methodName =jp.getSignature().getName(); //获取访问的方法的名称 Object[] args = jp.getArgs(); //获取访问的方法的参数 //获取具体执行的方法的Method对象 if(args==null || args.length == 0){ //无参数 executionMethod = executionClass.getMethod(methodName); //只能获取无参数方法 }else{ // 有参数,就将args中所有元素遍历,获取对应的Class,装入到一个Class[] Class[] classArgs = new Class[args.length]; for(int i=0; i<args.length; i++) { classArgs[i] = args[i].getClass(); } executionMethod = executionClass.getMethod(methodName, classArgs);// 获取有参数方法 } } //============================================================================================//
后置通知 的方法内容
//============================================================================================// //后置通知 //主要获取日志中其它信息,时长、ip、url @After("execution(* cn.bjut.ssm.controller.*.*(..))") public void doAfter(JoinPoint jp) throws Exception { // 获取类上的@RequestMapping对象 if (executionClass != SysLogController.class) { RequestMapping classAnnotation = (RequestMapping) executionClass.getAnnotation(RequestMapping.class); if (classAnnotation != null) { // 获取方法上的@RequestMapping对象 RequestMapping methodAnnotation = executionMethod.getAnnotation(RequestMapping.class); if (methodAnnotation != null) { String url = ""; // 它的值应该是类上的@RequestMapping的value+方法上的@RequestMapping的value url = classAnnotation.value()[0] + methodAnnotation.value()[0]; SysLog sysLog = new SysLog(); // 获取访问时长 Long executionTime = new Date().getTime() - startTime.getTime(); // 将sysLog对象属性封装 sysLog.setExecutionTime(executionTime); sysLog.setUrl(url); // 获取ip String ip = request.getRemoteAddr(); //已经在web.xml配置spring的request监听器 sysLog.setIp(ip); // 可以通过securityContext获取,也可以从request.getSession中获取 SecurityContext context = SecurityContextHolder.getContext(); //request.getSession().getAttribute("SPRING_SECURITY_CONTEXT") //String username = ((User) (context.getAuthentication().getPrincipal())).getUsername(); User user = (User) context.getAuthentication().getPrincipal(); String username = user.getUsername(); sysLog.setUsername(username); sysLog.setMethod("[类名]" + executionClass.getName() + "[方法名]" + executionMethod.getName()); sysLog.setVisitTime(startTime); // 调用Service,调用dao将sysLog insert数据库 sysLogService.save(sysLog); } } } }
=====================
end