1、首先是在spring配置文件下配置如下信息:
<!-- 切面 -->
<bean id="myAspectOfLoginAndLoginout" class="bea.com.aop.Log_loginAndLogout"></bean>
<bean id="myAspectOfAUD" class="bea.com.aop.Log_Record" >
<property name="userLogService">
<ref bean="userLogService" />
</property>
</bean>
<!-- 切入点 -->
<aop:config>
<aop:aspect id="log_login" ref="myAspectOfLoginAndLoginout">
<aop:after method="log_Login" pointcut="execution(* bea.com.admin.Service.*.Login*(..))" />
</aop:aspect>
<aop:aspect id="log_AUD" ref="myAspectOfAUD">
<aop:after-returning method="addUserLog" returning="returnValue" pointcut="execution(* bea.com.admin.Service.*.add*(..))" />
<aop:after-returning method="addUserLog" returning="returnValue" pointcut="execution(* bea.com.Service.*.add*(..))" />
<aop:after-returning method="modifyUserLog" returning="returnValue" pointcut="execution(* bea.com.Service.*.modify*(..))" />
<aop:after-returning method="modifyUserLog" returning="returnValue" pointcut="execution(* bea.com.admin.Service.*.modify*(..))" />
</aop:aspect>
</aop:config>
2、然后在aop类里实现如下代码:
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import bea.com.aop.service.UserLogService;
import bea.com.spojo.TuserLog;
import bea.com.util.DateParse;
import bea.com.util.reflectUtil;
public class Log_Record{
private static final Logger logger = LoggerFactory.getLogger(Log_Record.class);
private UserLogService userLogService;
public UserLogService getUserLogService() {
return userLogService;
}
public void setUserLogService(UserLogService userLogService) {
this.userLogService = userLogService;
}
/**
* 新增日志
* @param joinPoint
* @param returnValue
*/
public void addUserLog(JoinPoint joinPoint, Object returnValue){
Object[] args = joinPoint.getArgs();
String className="";//新增的类名
boolean flag=false;
int tableId=Integer.parseInt(returnValue.toString());
//joinPoint.getTarget();
//System.out.println("target:"+joinPoint.getTarget());
//target:bea.com.Service.CunWeiService@d7721e7
HttpSession session=ServletActionContext.getRequest().getSession();
int userId=Integer.parseInt(session.getAttribute("userId").toString());
System.out.println(args[0]);
//bea.com.spojo.Tcunweiandzhiwei@7470f191
Class userClass = args[0].getClass();
className=userClass.getSimpleName();
String datetimeNow=DateParse.date2String(new Date());
TuserLog tuserLog=new TuserLog();
tuserLog.setOperationTime(datetimeNow);
tuserLog.setOperationType(1);
tuserLog.setTableName(className);
tuserLog.setUserId(userId);
tuserLog.setUserId(userId);
tuserLog.setTableId(tableId);
this.userLogService.addUserLog(tuserLog);
System.out.println("returnValue"+returnValue);
}
/**
* 记录修改的日志
* @param joinPoint
*/
public void modifyUserLog(JoinPoint joinPoint,Object returnValue){
//如果修改成功!
if(Boolean.parseBoolean(returnValue.toString())){
Object[] args = joinPoint.getArgs();
String className="";//修改的类名
HttpSession session=ServletActionContext.getRequest().getSession();
int userId=Integer.parseInt(session.getAttribute("userId").toString());
System.out.println(args[0]);
Class userClass = args[0].getClass();
reflectUtil.reflect(args[0]);
className=userClass.getSimpleName();
String datetimeNow=DateParse.date2String(new Date());
TuserLog tuserLog=new TuserLog();
tuserLog.setOperationTime(datetimeNow);
tuserLog.setOperationType(1);
tuserLog.setTableName(className);
tuserLog.setUserId(userId);
tuserLog.setUserId(userId);
// tuserLog.setTableId(tableId);
this.userLogService.addUserLog(tuserLog);
//修改不成功
}else{
return;
}
}
}