数据库日志信息表
配置文件
在web.xml下配置监听器,用于获取Request,便于得到访问ip
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
由于该例子中抽取的是Controller中的方法,需要修改springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 扫描controller的注解,别的不扫描 -->
<context:component-scan base-package="com.itheima.ssm.controller">
</context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- JSP文件所在的目录 -->
<property name="prefix" value="/pages/" />
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 设置静态资源不过滤 -->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/plugins/" mapping="/plugins/**" />
<!-- 开启对SpringMVC注解的支持 -->
<mvc:annotation-driven />
<!--
支持AOP的注解支持,AOP底层使用代理技术
JDK动态代理,要求必须有接口
cglib代理,生成子类对象,proxy-target-class="true" 默认使用cglib的方式
-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
AOP代码
package com.czy.productAdmin.controller;
import com.czy.productAdmin.domain.SysLog;
import com.czy.productAdmin.service.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.UUID;
@Component
@Aspect
public class LogAop {
@Autowired
private SysLogService sysLogService;
@Autowired
private HttpServletRequest request; // 配置完监听器后即可注入
private Date visitTime; // 开始时间
private Class executionClass; // 访问的类
private Method executionMethod; // 访问的方法
//切面
@Pointcut("execution(* com.czy.productAdmin.controller.*.*(..))")
public void ppt() {}
//前置通知:主要是获取开始时间,执行的类是哪一个,访问哪一个方法
@Before("ppt()")
public void doBefore(JoinPoint jp) throws NoSuchMethodException {
visitTime = 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 {
Class[] classArgs = new Class[args.length];
for(int i = 0; i < args.length; ++i)
classArgs[i] = args[i].getClass();
executionMethod = executionClass.getMethod(methodName, classArgs);
}
}
//后置通知
@After("ppt()")
public void doAfter(JoinPoint jp) throws Exception {
if(executionClass == SysLogController.class) return;
// 获取访问时长
long executionTime = new Date().getTime() - visitTime.getTime();
// 获取访问的url
String url = "";
if(executionClass != null && executionMethod != null && LogAop.class != executionClass) {
RequestMapping classAnnotation = (RequestMapping) executionClass.getAnnotation(RequestMapping.class);
if(classAnnotation != null) {
String[] classValue = classAnnotation.value();
RequestMapping methodAnnotation = (RequestMapping) executionMethod.getAnnotation(RequestMapping.class);
if(methodAnnotation != null) {
String[] methodValue = methodAnnotation.value();
url = classValue[0] + methodValue[0];
// 获取访问的ip地址
String ip = request.getRemoteAddr();
// 获取操作者
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = user.getUsername();
// 将日志相关信息封装到SysLog对象里
SysLog sysLog = new SysLog();
sysLog.setId(UUID.randomUUID().toString());
sysLog.setVisitTime(visitTime);
sysLog.setUsername(username);
sysLog.setIp(ip);
sysLog.setUrl(url);
sysLog.setExecutionTime(executionTime);
sysLog.setMethod("[类名] " + executionClass.getName() + "[方法名] " + executionMethod.getName());
// 调用service存储信息
sysLogService.save(sysLog);
}
}
}
}
}
Dao代码
package com.czy.productAdmin.dao;
import com.czy.productAdmin.domain.SysLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface SysLogDao {
@Insert("insert into sysLog values(#{id}, #{visitTime}, #{username}, #{ip}, #{url}, #{executionTime}, #{method})")
public void save(SysLog sysLog) throws Exception;
@Select("select * from sysLog")
public List<SysLog> findAll() throws Exception;
}
Service代码
package com.czy.productAdmin.service.impl;
import com.czy.productAdmin.dao.SysLogDao;
import com.czy.productAdmin.domain.SysLog;
import com.czy.productAdmin.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SysLogServiceImpl implements SysLogService {
@Autowired
private SysLogDao sysLogDao;
@Override
public void save(SysLog sysLog) throws Exception {
sysLogDao.save(sysLog);
}
@Override
public List<SysLog> findAll() throws Exception {
return sysLogDao.findAll();
}
}
controller代码
package com.czy.productAdmin.controller;
import com.czy.productAdmin.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/sysLog")
public class SysLogController {
@Autowired
private SysLogService sysLogService;
@RequestMapping("/findAll.do")
public ModelAndView findAll() throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("syslog-list");
mv.addObject("sysLogs", sysLogService.findAll());
return mv;
}
}
标签:抽取,AOP,springframework,sysLog,org,import,日志,com,public
From: https://www.cnblogs.com/czy-algorithm/p/16951955.html