|
1 | 1 | package com.aspect;
|
2 | 2 |
|
3 |
| -import org.aspectj.lang.annotation.Aspect; |
4 |
| -import org.aspectj.lang.annotation.Before; |
| 3 | +import org.aspectj.lang.JoinPoint; |
| 4 | +import org.aspectj.lang.annotation.*; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
5 | 7 | import org.springframework.stereotype.Component;
|
| 8 | +import org.springframework.web.context.request.RequestContextHolder; |
| 9 | +import org.springframework.web.context.request.ServletRequestAttributes; |
| 10 | + |
| 11 | +import javax.servlet.http.HttpServletRequest; |
6 | 12 |
|
7 | 13 | /**
|
8 | 14 | * Created by zhuzhengping on 2017/3/19.
|
|
11 | 17 | @Component
|
12 | 18 | public class HttpAspect {
|
13 | 19 |
|
14 |
| - @Before("execution(public * com.controller.*(..))") |
| 20 | + private final static Logger LOGGER = LoggerFactory.getLogger(HttpAspect.class); |
| 21 | + |
| 22 | + @Pointcut("execution(public * com.controller.*(..))") |
15 | 23 | public void log(){
|
16 |
| - System.out.println("this is the aspect before every requset!!!!"); |
| 24 | + } |
| 25 | + |
| 26 | + @Before("log()") |
| 27 | + public void before(JoinPoint joinPoint){ |
| 28 | +// System.out.println("this is the aspect before every requset!!!!"); |
| 29 | + LOGGER.info("this is the aspect before every requset!!!!"); |
| 30 | + |
| 31 | + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| 32 | + HttpServletRequest request = attributes.getRequest(); |
| 33 | + |
| 34 | + //url |
| 35 | + LOGGER.info("url={}",request.getRequestURL()); |
| 36 | + //method |
| 37 | + LOGGER.info("method={}",request.getMethod()); |
| 38 | + //ip |
| 39 | + LOGGER.info("id={}",request.getRemoteAddr()); |
| 40 | + //class_method |
| 41 | + LOGGER.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName() + "," + joinPoint.getSignature().getName()); |
| 42 | + //args[] |
| 43 | + LOGGER.info("args={}",joinPoint.getArgs()); |
| 44 | + } |
| 45 | + |
| 46 | + @After("log()") |
| 47 | + public void after(){ |
| 48 | + LOGGER.info("this is the aspect after every response!!!"); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterReturning(pointcut = "log()",returning = "object") |
| 52 | + public void doAfterReturing(Object object){ |
| 53 | + LOGGER.info("response={}",object); |
17 | 54 | }
|
18 | 55 | }
|
0 commit comments