在使用AOP
環(huán)繞通知做日志處理的時(shí)候,發(fā)現(xiàn)@Around
方法執(zhí)行了兩次,雖然這里環(huán)繞通知本來(lái)就會(huì)執(zhí)行兩次,但是正常情況下是在切點(diǎn)方法前執(zhí)行一次,切點(diǎn)方法后執(zhí)行一次,但是實(shí)際情況卻是,切點(diǎn)方法前執(zhí)行兩次,切點(diǎn)方法后執(zhí)行兩次。
文字不好理解,還是寫一下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@Around ( "logPointCut()" ) public Object doAround(ProceedingJoinPoint pjp) throws Throwable { logger.debug( "==========Request log==========" ); long startTime = System.currentTimeMillis(); Object ob = pjp.proceed(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //JSONObject ipInfo = JSONObject.fromObject(URLDecoder.decode(WebUtils.getCookie(request,"IP_INFO").getValue(),"utf-8")); //logger.debug("IP: {}", ipInfo.get("ip")); //logger.debug("CITY {}", ipInfo.get("city")); logger.debug( "IP: {}" , BlogUtil.getClientIpAddr(request)); logger.debug( "REQUEST_URL: {}" , request.getRequestURL().toString()); logger.debug( "HTTP_METHOD: {}" , request.getMethod()); logger.debug( "CLASS_METHOD: {}" , pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName()); //logger.info("參數(shù) : " + Arrays.toString(pjp.getArgs())); logger.debug( "USE_TIME: {}" , System.currentTimeMillis() - startTime); logger.debug( "==========Request log end==========" ); return ob; } |
然后刷新一下頁(yè)面,得到的日志如下:
可以看到,雖然只刷新了一次,但是卻輸出了兩次日志,是不應(yīng)該的。然后通過(guò)斷點(diǎn)調(diào)試發(fā)現(xiàn),是因?yàn)樵?code>Controller中使用了@ModelAttribute
1
2
3
4
5
|
@ModelAttribute public void counter(Model model) { counter.setCount(counter.getCount() + 1 ); model.addAttribute( "count" , counter.getCount()); } |
@ModelAttribute
注解的方法會(huì)在Controller
方法執(zhí)行之前執(zhí)行一次,并且我將它放在了Controller
中,并且攔截的是所有Controller
中的方法,
這樣就導(dǎo)致了:
1、首先頁(yè)面請(qǐng)求到Controller
,執(zhí)行@ModelAttribute
標(biāo)注的方法,此時(shí)會(huì)被AOP
攔截到一次。
2、執(zhí)行完@ModelAttribute
標(biāo)注的方法后,執(zhí)行@RequestMapping
標(biāo)注的方法,又被AOP
攔截到一次。
所以,會(huì)有兩次日志輸出。
解決辦法:
1、將Controller
中的@ModelAttribute
方法,提取出來(lái),放到@ControllerAdvice
中。
2、對(duì)AOP
攔截規(guī)則添加注解匹配,例如:
1
|
execution( public * com.blog.controller.*.*(..)) && ( @annotation (org.springframework.web.bind.annotation.RequestMapping)) |
1
|
&& ( @annotation (org.springframework.web.bind.annotation.RequestMapping |
表明這樣只會(huì)攔截RequestMappping
標(biāo)注的方法。
注意:
如果是一個(gè)方法a()
調(diào)用同一個(gè)類中的方法b()
,如果對(duì)方法a()
做攔截的話,AOP
只會(huì)攔截到a()
,而不會(huì)攔截到b()
,因?yàn)榘?code>a()對(duì)b()的調(diào)用是通過(guò)this.b()
調(diào)用的,而AOP
正真執(zhí)行的是生成的代理類,通過(guò)this
自然無(wú)法攔截到方法b()
了。
了解Spring @Around使用及注意
注意:
1、Spring
切面注解的順序
-
@before
-
@Around
( 要代理的方法執(zhí)行在其中) -
@AfterReturning
-
@after
2、沒(méi)有@Around
,則 要代理的方法執(zhí)行 異常才會(huì)被@AfterThrowing
捕獲;
3、在@Around
如何執(zhí)行 要代理的方法執(zhí)行
1
2
3
4
5
6
7
8
9
10
11
|
@Around ( "execution(* cn.com.xalead.spring.MeInterface.*(..)) || execution(* cn.com.xalead.spring.KingInterface.*(..))" ) public Object test(ProceedingJoinPoint proceeding) { Object o = null ; try { //執(zhí)行 o = proceeding.proceed(proceeding.getArgs()); } catch (Throwable e) { e.printStackTrace(); } return o; } |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/wxgxgp/article/details/82526311