配置文件:
- <!-- 操作日志切面声明 -->
- <bean id="logAspect" class="com.tq365.service.sys.log.SystemLogAspect"/>
- <aop:config>
- <aop:aspect ref="logAspect">
- </aop:aspect>
- </aop:config>
实现代码:
- /**
- * 系统操作日志切面
- *
- * @author archie2010
- * since 2011-3-17 下午02:44:03
- */
- @Aspect
- public class SystemLogAspect {
- // int与long之Class会自动转为其封装类型之Class
- private static final String integerClazz = "class java.lang.Integer";
- private static final String longClazz = "class java.lang.Long";
- @Resource
- private SystemLogService systemLogService;
- private Logger logger = Logger.getLogger(this.getClass().getName());
- @Pointcut("execution(* com.tq365.service..*.*(..))")
- public void myAspect() {
- };
- @AfterThrowing(pointcut = "myAspect()", throwing = "e")
- public void doAfterThrowing(JoinPoint jp, Throwable e) {
- System.out.println("出现异常:" + e.getMessage());
- System.out.println(e.getClass().getName());
- System.out.println("异常所在类:" + jp.getTarget().getClass().getName());
- System.out.println("" + jp.getSignature().getName()
- + "方法 throw exception");
- // logger.error("错误! error级别的!!!"+e.getMessage());
- logger.error("Oops===" + jp.getTarget().getClass().getName() + "中的"
- + jp.getSignature().getName() + "方法抛出" + e.getClass().getName()
- + "异常");
- System.out.println("参数:");
- ;
- if (jp.getArgs() != null && jp.getArgs().length > 0) {
- for (int i = 0; i < jp.getArgs().length; i++) {
- System.out.println(jp.getArgs()[i].toString());
- logger.error("参数:--" + jp.getArgs()[i].toString());
- }
- }
- }
- @SuppressWarnings("unchecked")
- @After("@annotation(com.tq365.sys.annotation.SystemLogAnnotation)")
- public void doAfter(JoinPoint jp) {
- System.out.println("----------后置通知");
- System.out.println("方法所在类:" + jp.getTarget().getClass().getName());
- System.out.println("" + jp.getSignature().getName() + "方法");
- String methodName = jp.getSignature().getName();
- // 操作日志对象-----------------
- SystemLog sysLog = new SystemLog();
- // 操作参数-----------------
- String descArgs = "参数";
- if (jp.getArgs() != null && jp.getArgs().length > 0) {
- for (int i = 0; i < jp.getArgs().length; i++) {
- if(jp.getArgs()[i]!=null){
- //System.out.println(jp.getArgs()[i].toString());
- descArgs += jp.getArgs()[i].toString()+",";
- }else{
- descArgs +="null"+",";
- }
- }
- System.out.println("------参数" + descArgs);
- }
- sysLog.setOperateArgs(descArgs);
- String des = null;//方法描述
- if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {
- Class targetClass = jp.getTarget().getClass();
- //方法不定向参数Clazz...
- Class[] claszs = new Class[jp.getArgs().length];
- for (int i = 0; i < jp.getArgs().length; i++) {
- //System.out.println(jp.getArgs()[i]);
- if(jp.getArgs()[i]!=null){
- System.out.println(jp.getArgs()[i].getClass());
- if (jp.getArgs()[i].getClass().toString().equals(integerClazz)) {
- claszs[i] = int.class;
- } else if (jp.getArgs()[i].getClass().toString().equals(
- longClazz)) {
- claszs[i] = long.class;
- }else{
- claszs[i] =jp.getArgs()[i].getClass();
- }
- }else if(jp.getArgs()[i]==null){
- claszs[i] = String.class;
- }
- }
- Method method=null;
- try {
- method = targetClass.getMethod(methodName, claszs);
- } catch (SecurityException e) {
- } catch (NoSuchMethodException e) {
- }
- //若方法为空(描述无法获得则des=null)
- if(method!=null){
- System.out.println(method.getAnnotation(SystemLogAnnotation.class)
- .description());
- des = method.getAnnotation(SystemLogAnnotation.class).description();
- }
- }
- // 获得Session
- HttpSession session = ServletActionContext.getRequest().getSession();
- // 取到当前的操作用户
- User appUser = (User) session.getAttribute("USER");
- if (appUser != null) {
- System.out.println("用户已经存在Session中");
- // 操作日志对象
- sysLog.setUid(appUser.getUserId());
- sysLog.setUsername(appUser.getFullName());
- }
- HttpServletRequest request = ServletActionContext.getRequest();
- String ip = request.getRemoteAddr();
- sysLog.setOperateTime(DateUtil.getCurrentTime());
- sysLog.setOperateDes(methodName +"->"+ des);
- sysLog.setIp(ip);
- systemLogService.save(sysLog);
- System.out.println("----------保存操作日志");
- }
- }