此博客用于个人学习,来源于ssm框架的书籍,对知识点进行一个整理。
第四章 SSM AOP 日志:
1. 数据库与表结构:
1.1 日志表信息描述 sysLog:
序号 | 字段名称 | 字段类型 | 字段描述 |
---|---|---|---|
1 | id | varchar2 | 主键 无意义uuid |
2 | visitTime | timestamp | 访问时间 |
3 | username | varchar2 | 操作者用户名 |
4 | ip | varchar2 | 访问ip |
5 | url | varchar2 | 访问资源url |
6 | executionTime | int | 执行时长 |
7 | method | varchar2 | 访问方法 |
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 实体类 SysLog:
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;
//其他get/set方法
}
2. 基于 AOP 日志处理:
创建日志查看页面 syslog-list.jsp。
2.1 创建切面类处理日志:
在 web 内创建一个 LogAop 类,使用注解 @Aspect 定义为切面,注入 request 与 SysLogService,定义 before 方法,用于获取时间,类和方法,定义 after 方法,用于获取其他信息——时长,ip 和 url,这个相当于其他操作的在 controller 类的添加方法。
@Component
@Aspect
public class LogAop {
@Autowired
private HttpServletRequest request;
@Autowired
private ISysLogService sysLogService;
private Date visitTime; //开始时间
private Class clazz; //访问的类
private Method method;//访问的方法
//前置通知 主要是获取开始时间,执行的类是哪一个,执行的是哪一个方法
@Before("execution(* com.itheima.ssm.controller.*.*(..))")
public void doBefore(JoinPoint jp) throws NoSuchMethodException {
visitTime = new Date();//当前时间就是开始访问的时间
clazz = jp.getTarget().getClass(); //具体要访问的类
String methodName = jp.getSignature().getName(); //获取访问的方法的名称
Object[] args = jp.getArgs();//获取访问的方法的参数
//获取具体执行的方法的Method对象
if (args == null || args.length == 0) {
method = clazz.getMethod(methodName); //只能获取无参数的方法
} else {
Class[] classArgs = new Class[args.length];
for (int i = 0; i < args.length; i++) {
classArgs[i] = args[i].getClass();
}
clazz.getMethod(methodName, classArgs);
}
}
//后置通知
@After("execution(* com.itheima.ssm.controller.*.*(..))")
public void doAfter(JoinPoint jp) throws Exception {
long time = new Date().getTime() - visitTime.getTime(); //获取访问的时长
String url = "";
//获取url
if (clazz != null && method != null && clazz != LogAop.class) {
//1.获取类上的@RequestMapping("/orders")
RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
if (classAnnotation != null) {
String[] classValue = classAnnotation.value();
//2.获取方法上的@RequestMapping(xxx)
RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
if (methodAnnotation != null) {
String[] methodValue = methodAnnotation.value();
url = classValue[0] + methodValue[0];
//获取访问的ip
String ip = request.getRemoteAddr();
//获取当前操作的用户
SecurityContext context = SecurityContextHolder.getContext();//从上下文中获了当前登录的用户
User user = (User) context.getAuthentication().getPrincipal();
String username = user.getUsername();
//将日志相关信息封装到SysLog对象
SysLog sysLog = new SysLog();
sysLog.setExecutionTime(time); //执行时长
sysLog.setIp(ip);
sysLog.setMethod("[类名] " + clazz.getName() + "[方法名] " + method.getName());
sysLog.setUrl(url);
sysLog.setUsername(username);
sysLog.setVisitTime(visitTime);
//调用Service完成操作
sysLogService.save(sysLog);
}
}
}
}
}
注意:
- username 通过 SecurityContextHolder 获取。
- ip 地址的获取通过 request.getRemoteAddr() 方法获取到,在 Spring 中可以通过RequestContextListener 来获取 request 或 session 对象。
2.2 Controller:
定义 SysLogController 类,注入 SysLogService,添加方法 findAll。
@Controller
@RequestMapping("/sysLog")
public class SysLogController {
@Autowired
private ISysLogService sysLogService;
@RequestMapping("/findAll.do")
public ModelAndView findAll() throws Exception {
ModelAndView mv=new ModelAndView();
List<SysLog> sysLogList= sysLogService.findAll();
mv.addObject("sysLogs",sysLogList);
mv.setViewName("syslog-list");
return mv;
}
}
2.3 Service:
定义 SysLogService 接口及其实现类,注入 SysLogService,添加 save 和 findAll 方法。
@Service
@Transactional
public class SysLogServiceImpl implements ISysLogService {
@Autowired
private ISysLogDao sysLogDao;
@Override
public List<SysLog> findAll() throws Exception {
return sysLogDao.findAll();
}
@Override
public void save(SysLog sysLog) throws Exception {
sysLogDao.save(sysLog);
}
}
2.4 Dao:
定义 ISysLogDao 类,添加 findAll 和 save 方法。
public interface ISysLogDao {
@Insert("insert into syslog(visitTime,username,ip,url,executionTime,method) values(#{visitTime},#{username},#{ip},#{url},#{executionTime},#{method})")
public void save(SysLog sysLog) throws Exception;
@Select("select * from sysLog")
List<SysLog> findAll() throws Exception;
}