if(datas.get(i).getId()==person.getId()){
datas.set(i,person);
return1;
}
}
return0;
}
publicstaticintremove(intindex){
try{
datas.remove(index);
return1;
}catch(Exceptione){
e.printStackTrace();
return0;
}
}
//往数据库里面添加日志
publicstaticintaddLog(OperateRecordoperateRecord){
try{
logs.add(operateRecord);
return1;
}catch(Exceptione){
e.printStackTrace();
return0;
}
}
publicstaticListqueryAllLogs(){
returnlogs;
}
//用于模拟生成ID
publicsynchronizedstaticintgetId(){
returnid++;
}
6.修改之前TestService接口以及实现类
publicinterfaceTestService{
publicPersonget(intid);
publicListlist();
publicintadd(Personerson);
publicintupdate(Personperson);
publicintdelete(intid);
}
publicclassTestServiceImplimplementsTestService{
//目标对象(添加,修改时候用到)
privatePersonbean;
//原始对象(修改时候用到)
privatePersonoldBean;
@OperateLoger(content="查询单个对象操作",operationType=OperationType.R,
noticeSystem=NoticeSystem.IN)
publicPersonget(intid){
returnDataBase.get(id);
}
@OperateLoger(content="查询List操作",operationType=OperationType.R,
noticeSystem=NoticeSystem.IN)
publicListlist(){
returnDataBase.list();
}
@OperateLoger(content="添加操作",operationType=OperationType.C,
noticeSystem=NoticeSystem.ALL)
publicintadd(Personperson){
returnDataBase.add(person);
}
@OperateLoger(content="更改操作",operationType=OperationType.U,
noticeSystem=NoticeSystem.ALL)
publicintupdate(Personperson){
returnDataBase.update(person);
}
@OperateLoger(content="删除操作",operationType=OperationType.D,
noticeSystem=NoticeSystem.ALL)
publicintdelete(intid){
returnDataBase.remove(id);
}
//省略gettersetter方法
}
7.重点改写LogerAspect日志处理类(涉及到的工具类这里省略)
@Aspect
@Component
publicclassLogerAspect{
@Pointcut("execution(*com.manager.*.service.*.*(..))")
publicvoidlogAspect(){
}
@Before(value="logAspect()&&@annotation(ol)")//注意这里的变化
publicsynchronizedvoidbeforeOperateLog(JoinPointjp,OperateLogerol){
//记录日志暂时不考虑前置通知……
}
@After("logAspect()&&@annotation(ol)")
publicsynchronizedvoidaddOperateLog(JoinPointjp,OperateLogerol){
System.out.println("调用后置通知……");
OperateRecordoperateRecord=newOperateRecord();
try{
StringIp=NetworkTools.getIpAddrByRequest();
StringopertionType=ol.operationType().toString();
operateRecord.setId(DataBase.getId()+"");
operateRecord.setUserId("userId");//测试时候直接写死一个userid字符串
operateRecord.setRsrcCode(ObjectUtils.getObjectName(jp.getSignature().getDeclaringTypeName())+"_"+jp.getSignature().getName());
operateRecord.setOpType(opertionType);
operateRecord.setOpDesc(ol.content());
operateRecord.setOpResult("true");
operateRecord.setClientIp(Ip);
operateRecord.setParams(Arrays.toString(jp.getArgs()));
operateRecord.setOpTime(newRandom().nextInt(1000)+"");//这里用随机数表示,实际操作时间需要计算
operateRecord.setCreateTime(newDate());
operateRecord.setLastUpdTime(newDate());
//接下来查找更新时候,对象变化的字段
Objectobj=jp.getThis();
ObjecttargetObject=null;
MaptargetMap=null;
if(ol.targetObject()!
=null&&!
"".equals(ol.targetObject())){
MethodtargetMethod=null;
try{
targetMethod=obj.getClass().getMethod("get"+ol.targetObject());
}catch(Exceptione){
e.printStackTrace();
}
if(targetMethod==null){
//如果没有方法的话,就给个默认的
targetMap=newHashMap();
}else{
targetObject=targetMethod.invoke(obj);
targetObject=AopTargetUtils.getTarget(targetObject);
targetMap=ObjectUtils.getObjectValue(targetObject);
}
}
MapoldMap=null;
Stringdif[]=newString[2];
if("C".equals(opertionType)||"R".equals(opertionType)||
"D".equals(opertionType)){
if(ol.targetObject()!
=null&&!
"".equals(ol.targetObject())){
dif[1]=ObjectUtils.convertMapToString(targetMap);
}
dif[0]="";
}else{
MethodoldMethod=obj.getClass().getMethod("get"+ol.oldObject());
ObjectoldObject=oldMethod.invoke(obj);
oldObject=AopTargetUtils.getTarget(oldObject);
oldMap=ObjectUtils.getObjectValue(oldObject);
dif=ObjectUparisonMap(oldMap,targetMap);
}
operateRecord.setPrevContent(dif[0]);
operateRecord.setContent(dif[1]);
Stringtables=ol.tables();//受影响的表(其他相关处理)
DataBase.addLog(operateRecord);//模拟写入数据库
}catch(Exceptione){
e.printStackTrace();
}
}
8.添加单元测试方法
publicclassTestModel{
ClassPathXmlApplicationContextcontext;
TestServiceservice;
@Before
publicvoidbefore(){
context=newClassPathXmlApplicationContext("application-context.xml");
service=(TestService)context.getBean("testService");
}
@After
publicvoidafter(){
System.out.println("查看日志......");
Listops=DataBase.queryAllLogs();
for(OperateRecord