首先我们先创建一个日志注解的类
package com.ssh.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 一个日志注解的类
* @author Administrator
*
*/
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemLog {
/**
* 日志描述
* 对人做了什么操作进行记录
*/
String description() default "";
}
之后我们在创建一个切面通知类
package com.ssh.aop;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionContext;
import com.ssh.log.biz.LogsBiz;
import com.ssh.log.entity.Logs;
import com.ssh.permission.entity.User;
/**
* 系统日志 切面通知类
* @author Administrator
*
*/
@Aspect
@Component
public class LogAspect {
private LogsBiz logsBiz;
public LogsBiz getLogsBiz() {
return logsBiz;
}
public void setLogsBiz(LogsBiz logsBiz) {
this.logsBiz = logsBiz;
}
/**
* 环绕通知
*
* @param joinPoint
* @return
* @throws Throwable
*/
public Object aroud(ProceedingJoinPoint joinPoint) throws Throwable {
// 开始时间
long beginTime = System.currentTimeMillis();
// 执行目标方法
Object result = joinPoint.proceed();
// 执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
// 保存日志
saveSystemLog(joinPoint, result, time);
//返回执行方法
return result;
}
/**
* 保存日志
* @param joinPoint
* @param result
* @param time
*/
private void saveSystemLog(ProceedingJoinPoint joinPoint, Object result, long time) {
// TODO Auto-generated method stub
//獲取session
Map<String,Object> session = (Map<String,Object>)ActionContext.getContext().getSession();
//得到session中当前登录的用户
Object obj = session.get("user");
// 获取当前操作的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
/**
下面是我数据库的一些表字段
log_id 日志ID
user_name 操作用户名
user_do 用户实际操作
menthd 调用的方法
params 主要的参数
craet_time 操作时间
**/
Logs logs = new Logs();
//获取注解类
SystemLog systemLog = method.getAnnotation(SystemLog.class);
if(systemLog != null) {
//把注解类中日志描述加到集合中去
logs.setUser_do(systemLog.description());
}
// 获取目标类名
String className = joinPoint.getTarget().getClass().getName();
//获取类名
String methodName = signature.getName();
logs.setMethod(className+"."+methodName);
// 请求的参数
Object[] args = joinPoint.getArgs();
if (args != null && args.length != 0 && args[0] != null) {
logs.setParams(args[0].toString());
}
//获取操作用户(当第一次打开浏览器 session里面是没有值的 所以就从参数里面获取用户名,不然就直接获取session里面的值)
if(obj != null) {
User userName = (User)obj;
logs.setUser_name(userName.getUsr_name());
}else {
logs.setUser_name(((User)args[0]).getUsr_name());
}
//操作时间(转换时间)
SimpleDateFormat sif=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
logs.setCreat_time(sif.format(new Date()));
//保存系统日志
try {
logsBiz.add_logs(logs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
再者我们就要写 AspectJ配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- -->
<bean class="com.ssh.log.dao.LogsDao" id="logsDao" parent="basedao"></bean>
<bean class="com.ssh.log.biz.impl.LogsImpl" id="logsBiz" parent="baseBiz">
<!-- 将logsDao注入进来 -->
<property name="logsDao" ref="logsDao"></property>
</bean>
<bean class="com.ssh.log.web.LogsAction" id="logsAction" parent="baseAction">
<!-- 将logsBiz注入进来 -->
<property name="logsBiz" ref="logsBiz"></property>
</bean>
<!-- 切面类 -->
<bean class="com.ssh.aop.LogAspect" id="logAspect">
<!-- 将Logbiz注入劲进来 -->
<property name="logsBiz" ref="logsBiz"></property>
</bean>
<aop:config>
<!-- 切面 -->
<aop:aspect ref="logAspect">
<!-- 定义切入点 -->
<aop:pointcut expression="@annotation(com.ssh.aop.SystemLog)" id="lpt"/>
<!-- 环绕通知 -->
<aop:around method="aroud" pointcut-ref="lpt"/>
</aop:aspect>
</aop:config>
</beans>
在AspectJ配置文件中定义了切点的切入位置为加了@SystemLog注解的方法中(即标有@SystemLog注解的方法才会保存到数据库中)
再者展现的效果为:
我这也就只能写简单版的了(??)