建体彩网|中彩网双色球连号|
?
快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

偉德國際始于英國1946:采用spring AOP 和注解解決日志

?

起開創建一個自定義評釋

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface RecordLog {

}

然后采納spring Aop

@Component("logAround")

public class LogAroundInterceptor implements MethodInterceptor {

@Autowired

private SysLogService sysLogService;

private final Logger logger = Logger.getLogger(LogAroundInterceptor.class);

public Object invoke(MethodInvocation invocation) throws Throwable {

// TODO Auto-generated method stub

Object returnObject = invocation.proceed();

Method method = invocation.getMethod();

if (method.isAnnotationPresent(RecordLog.class)) {

Object[] arguments = invocation.getArgume偉德國際始于英國1946nts();

Object saveObject = null;

SysLog sysLog = new SysLog();

if (null != arguments) {

saveObject = argumen偉德國際始于英國1946ts[0];

sysLog = getSysLog(getOperaType(method.getName()), saveObject);偉德國際始于英國1946

sysLogService.saveSysLog(sysLog);

}

}

return returnObject;

}

public int getOperaType(String methodName) {

int type = 0;

if (methodName.startsWith("save") || methodName.startsWith("add")

|| methodName.startsWith("insert")

|| methodName.startsWith("update")) {

type = 1;

} else if (methodName.startsWith("delete")

|| methodName.startsWith("del")) {

type = 2;

}

return type;

}

public SysLog getSysLog(int type, Object object) {

SysLog sysLog = new SysLog();

sysLog.setCreateTime(new Date());

StringBuffer sysContent = new StringBuffer();

if (type != 1 && type != 2) {

logger.warn("此措施不能被記錄日志");

return null;

}

if (object instanceof User) {

User user = (User) object;

sysContent.append("用戶:");

if (type == 1) {

sysContent.append(user.getUsername()).append("被保存.");

} else if (type == 2) {

sysContent.append(user.getDeleteUser()).append(

"刪除了用戶:" + user.getUniqueUserName());

}

} else if (object instanceof Tribe) {

Tribe tribe = (Tribe) object;

if (type == 1) {

sysContent.append("用戶:").append(

tribe.getUser().getUniqueUserName()).append("保存了部落:")

.append(tribe.getName());

} else if (type == 2) {

sysContent.append("用戶:").append(tribe.getDeleteUser()).append(

"刪除了部落:").append(tribe.getUniqueTribeName());

}

} else if (object instanceof Assessment) {

Assessment assessment = (Assessment) object;

int assType = assessment.getTy偉德國際始于英國1946pe();

User user = assessment.getUserByUs偉德國際始于英國1946erId();

sysContent.append("用戶:").append(user.getUniqueUserName()).append(

"舉報了");

if (assType == 1) {

sysContent.append("資本").append(

assessment.getResource().getTitle());

} else if (assType == 2) {

sysContent.append("部落").append(

assessment.getTribeByAccTribeId().getUniqueTribeName());

} else if (assType == 3) {

sysContent.append("人").append(

assessment.getUserByUserId().getUniqueUserName());

}

} else if (object instanceof Role) {

Role role = (Role) object;

sysContent.append("用戶:").append(role.getOperaUser());

if (type == 1) {

sysContent.append("保存了").append(role.getRoleName());

} else if (type == 2) {

sysContent.append("刪除了").append(role.getRoleName());

}

} else if (object instanceof Resource) {

Resource res = (Resource) object;

boolean isPass = res.getAppStatus() == 1;

sysContent.append("一級審核職員:").append(res.getAppUser().getUniqueUserName());

sysContent.append("審核").append(res.getUniqueResName());

if (!isPass) {

sysContent.append("未經由過程");

} else if (isPass) {

sysContent.append("經由過程");

}

}

logger.info(sysContent.toString());

sysLog.setContent(sysContent.toString());

return sysLog;

}

}

在必要插入評釋的措施上面寫上@RecordLog就可以啦

轉自:http://kaowww153.javaeye.com/blog/603891

免責聲明:以上內容源自網絡,版權歸原作者所有,如有侵犯您的原創版權請告知,我們將盡快刪除相關內容。

您可能還會對下面的文章感興趣:

建体彩网
2015年彩票大奖 高频彩交易 如何看足彩盘口赔率 22选5开奖结果第86期 幸运28单双预测 吉林时时彩玩法 老时时彩 贵州快3 一波中特猪在家 35即时比分