SpringBoot 는 AOP 를 어떻게 우아 하 게 사용 합 니까?
spring-boot 의 op 의존 도 를 추가 합 니 다.
org.springframework.boot
spring-boot-starter-aop
@Aspect
@Component
public class HttpRequestAspect {
private static final Logger log = LoggerFactory.getLogger(HttpRequestAspect.class);
public static long startTime;
public static long endTime;
/*@PointCut , */
/* */
@Pointcut("execution(public * com.simons.cn.springbootdemo.controller.*.*(..))")
/* */
public void print() {
}
/*@Before */
@Before("print()")
public void before(JoinPoint joinPoint) {
log.info(" before……");
startTime = System.currentTimeMillis();
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String requestURI = request.getRequestURI();
String remoteAddr = request.getRemoteAddr(); // ip" "
String requestMethod = request.getMethod();
String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
log.info(" url=" + requestURI + ", ip=" + remoteAddr + ", =" + requestMethod + ", =" + declaringTypeName + ", =" + methodName + ", =" + args);
}
/*@After */
@After("print()")
public void after() {
endTime = System.currentTimeMillis() - startTime;
log.info(" after……");
}
/*@AfterReturning */
@AfterReturning(pointcut = "print()", returning = "object")
public void getAfterReturn(Object object) {
log.info(" ={}ms", endTime);
log.info("afterReturning={}", object.toString());
}
}
1. @PointCut(×××) , @Before、@After、@AfterReturn , @Before(value="print()") , ;
2.execution(public * com.simons.cn.springbootdemo.controller.*.*(..)) :
* , *.* , ..
:
@GetMapping(value = "/index")
@ResponseBody
public String index() {
ActivitySystemVariable systemVariable = activitySystemVariableMapper.selectByPrimaryKey(258);
return systemVariable.toString();
}
,ok
2018-06-28 11:21:10.939 INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect : before……
2018-06-28 11:21:10.939 INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect : url=/index, ip=0:0:0:0:0:0:0:1, =GET, =com.simons.cn.springbootdemo.controller.IndexController, =index, =[Ljava.lang.Object;@1efabee
2018-06-28 11:21:10.951 INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect : after……
2018-06-28 11:21:10.951 INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect : =12ms
2018-06-28 11:21:10.951 INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect : afterReturning=ActivitySystemVariable{id=258, name='simonsfan ', svKey='simons-key', value='simons-value', memo='simonsfan ', typeCode='', cataLog=0, createTime=2018-06-28 11:18:19.0}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.