springAop整合自定义注解做方法日志配置(源码在附件)
Posted Danial7777777
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springAop整合自定义注解做方法日志配置(源码在附件)相关的知识,希望对你有一定的参考价值。
package com.aop.log.anno; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)//运行期 @Target({ElementType.METHOD,ElementType.TYPE})//作用于方法上,类上 public @interface Mylog { public String value() default "";//日志信息值 boolean ingore() default false;//是否忽略 }
package com.aop.log.aspect; import com.aop.log.anno.Mylog; import org.apache.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; @Component @Aspect public class LogAspect { private Logger logger= Logger.getLogger(LogAspect.class); private static final String dateFormat="yyyy-MM-dd HH:mm:ss"; @Pointcut(value = "execution(* com.aop.log.dao.*.*(..))") public void pointCut1(){} @Around(value = "LogAspect.pointCut1()") public Object aroundLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { //日志信息 StringBuffer loginfo=new StringBuffer(); //获得方法签名 MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature(); //获得方法对象 Method method = methodSignature.getMethod(); //获得字节码对象 Class<?> clazz = method.getDeclaringClass(); //获得类上注解 Mylog mylog_anno_clazz = clazz.getAnnotation(Mylog.class); //获得方法上注解 Mylog mylog_anno_method = method.getAnnotation(Mylog.class); //方法上参数 Object[] args = proceedingJoinPoint.getArgs(); //判断类上是否有注解 if (mylog_anno_clazz!=null){ if (mylog_anno_clazz.ingore()) { //忽略日志 直接执行方法 return proceedingJoinPoint.proceed(args); } } //判断方法上是否有注解 if (mylog_anno_method!=null){ if (mylog_anno_method.ingore()){ //忽略日志 直接执行方法 return proceedingJoinPoint.proceed(args); } } //拼凑日志信息 String target=clazz.getName()+"##############"+method.getName();//类名+方法名 //参数 StringBuffer params=new StringBuffer(); if (args.length>0){ for (Object arg : args) { params.append(","+arg.getClass().getName()); } params.substring(1); } //日志信息 loginfo.append(target+params); //获得起始时间 SimpleDateFormat sdf=new SimpleDateFormat(dateFormat); String start = sdf.format(new Date()); //日志方法 前置通知 logger.error("开始调用"+loginfo+"时间:"+start); long currentTimeMillis = System.currentTimeMillis(); //方法执行 Object proceed = proceedingJoinPoint.proceed(args); long currentTimeMillis2 = System.currentTimeMillis(); //方法耗时 long wastetime=((currentTimeMillis2-currentTimeMillis)/1000); String end = sdf.format(new Date()); logger.error("方法耗时:"+wastetime+"s"+"时间:"+end); return proceed; } }
package com.aop.log.bean; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", name=‘" + name + ‘‘‘ + ‘}‘; } }
package com.aop.log.dao; import com.aop.log.bean.User; //@Mylog(value = "user dao log",ingore = true) //@Mylog(value = "user dao log",ingore = false) public interface UserDao { //@Mylog(value = "user dao log",ingore = false) //@Mylog(value = "user dao log",ingore = true) public void save(User user) throws InterruptedException; public void delete(); public void update(); public void find(); }
package com.aop.log.dao; import com.aop.log.bean.User; import org.springframework.stereotype.Repository; @Repository("userDaoImpl") public class UserDaoImpl implements UserDao { @Override public void save(User user) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(user); } @Override public void delete() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("delete user!!!"); } @Override public void update() { System.out.println("update user!!!"); } @Override public void find() { System.out.println("find user!!!"); } }
package com.aop.log.test; import com.aop.log.bean.User; import com.aop.log.dao.UserDao; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml" ) public class Test { @Resource(name = "userDaoImpl") private UserDao userDaoImpl; @org.junit.Test public void test1() { User user=new User(); user.setId(1); user.setName("abc"); //userDaoImpl.save(user); userDaoImpl.delete(); } }
以上是关于springAop整合自定义注解做方法日志配置(源码在附件)的主要内容,如果未能解决你的问题,请参考以下文章
自定义注解结合SpringAop实现权限,参数校验,日志等等功能