快捷搜索:  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

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: