Skip to content

Commit 770a22e

Browse files
aspect_pointcut
1 parent 1b9ed88 commit 770a22e

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed
Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package com.aspect;
22

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;
57
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;
612

713
/**
814
* Created by zhuzhengping on 2017/3/19.
@@ -11,8 +17,39 @@
1117
@Component
1218
public class HttpAspect {
1319

14-
@Before("execution(public * com.controller.*(..))")
20+
private final static Logger LOGGER = LoggerFactory.getLogger(HttpAspect.class);
21+
22+
@Pointcut("execution(public * com.controller.*(..))")
1523
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);
1754
}
1855
}

0 commit comments

Comments
 (0)