1、pom.xml中引入以下依赖
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency>
2、在common工程中创建aspect包,如下
3、创建CommonLog接口,内容如下
4、创建CommonLogAspect.java类,内容如下
@Aspect //代表这是一个切面类 @Component //注入到Spring ioc @Slf4j public class CommonLogAspect { @Pointcut("execution(public * com.bj.*.controller.*.*(..))") //controller包下的所有方法都打日剧 public void CommonLogAspec(){ } @Before("CommonLogAspec()") public void doBefore(JoinPoint joinPoint){ log.info("方法执行前..."); ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if(sra != null){ HttpServletRequest request = sra.getRequest(); log.info("ip:" + request.getRemoteHost()); log.info("url:" + request.getRequestURI()); log.info("method:"+request.getMethod()); log.info("class.method:" + joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName()); log.info("args:" + joinPoint.getArgs()); } log.info("----------AOP日志end---------------"); }
即:
5、使用方法
在想要添加注解的方法上加上注解
6、测试
使用浏览器测试