package com.sumavision.bnadmin.util;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>
* 关于<b>Action</b>的说明
* </p>
* 类的方法描述注解
*
* @author gavin
* @version 1.0
* @since BN-Admin 1.0
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface LogAction {
public String servicename() default "no servicename";
public String methodname() default "no methodname";
public String description() default "no description";
}
-----------------------------------------华丽的分割线--------------------------------------------
package com.sumavision.bnadmin.util;
import java.lang.reflect.Method;
import java.util.Date;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import com.sumavision.bnadmin.admin.service.UseroperateService;
import com.sumavision.bnadmin.domain.entity.BnUseroperate;
/**
* <p>
* 关于<b>LogAspect</b>的说明
* </p>
* 服务日志
*
* @author gavin
* @version 1.0
* @since BN-Admin 1.0
*
*/
@Aspect
public class LogAspect {
/**
* <p>
* 属性useroperateService
* </p>
* 服务日志维护服务
*/
@Resource
private UseroperateService useroperateService;
public UseroperateService getUseroperateService() {
return useroperateService;
}
public void setUseroperateService(UseroperateService useroperateService) {
this.useroperateService = useroperateService;
}
private Log logger = LogFactory.getLog(LogAspect.class);
@Around("execution(* com.sumavision.bnadmin.admin.web..*(..))")
public Object doSystemLog(ProceedingJoinPoint point) throws Throwable {
Object retVal = point.proceed();
String methodName = point.getSignature().getName();
// 目标方法不为空
if (StringUtils.isNotEmpty(methodName)) {
// set与get方法除外
if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {
Class targetClass = point.getTarget().getClass();
Method method = targetClass.getMethod(methodName);
if (method != null) {
boolean hasAnnotation = method
.isAnnotationPresent(LogAction.class);
if (hasAnnotation) {
LogAction annotation = method.getAnnotation(LogAction.class);
String servicename = annotation.servicename();
String methodname = annotation.methodname();
String description = annotation.description();
if (logger.isDebugEnabled()) {
logger.debug("Action method:" + method.getName()
+ " Description:" + description);
}
// 取到当前的操作用户
String j_username = (String) ServletActionContext
.getServletContext().getAttribute("j_username");
if (j_username != null) {
try {//调用服务将信息记录到数据库
BnUseroperate useroperate = new BnUseroperate();
useroperate.setUsername(j_username);
useroperate.setServicemethodname(methodname);
useroperate.setServicename(servicename);
useroperate.setLastedittime(new Date());
useroperate.setDescription(description);
useroperateService.addUseroperate(useroperate);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
return retVal;
}
}
-----------------------------------------优雅的分割线--------------------------------------------
@LogAction(servicename="登录",methodname="login",description="管理员登录")
public String login() {
ServletActionContext.getServletContext().setAttribute("j_username", j_username);
try {
String encodePassword = passwordEncoder.encodePassword(j_password, null);
boolean login_error = userService.login(j_username, encodePassword);
ServletActionContext.getRequest().setAttribute("login_error", login_error);
if (false == login_error) {
return ERROR;
}
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
return SUCCESS;
}