Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are @ Pointcut and @ Around in springboot aspect

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What are @ Pointcut and @ Around in springboot aspect? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Background

Based on springboot2.X version

Combined with https://my.oschina.net/ysma1987/blog/597601 second edition update

2. For AOP section

Org.springframework.boot spring-boot-starter-aop

3. It's useless to say more, just go to the code.

Package com.ysma.webconfig.filter;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.ysma.entity.EsLogDto;import com.ysma.exception.CustomException;import com.ysma.intf.EsLog;import com.ysma.util.SnowFlake;import org.apache.commons.lang3.time.StopWatch;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.slf4j.Logger;import org.slf4j.LoggerFactory Import org.springframework.http.HttpStatus;import org.springframework.stereotype.Component;import java.util.Date;import java.util.UUID;import java.util.concurrent.TimeUnit;@Aspect@Componentpublic class LogAspect {/ / define ES log private static final Logger LOGGER = LoggerFactory.getLogger ("RES_REQ_LOG_ES_APPENDER") / / define expressions where within is used. Note that esLog does not need the full path because it is a parameter private final String POINT_CUT = "within (com.ysma.appliaction.service..*) & & @ annotation (esLog)". / / define pointcut pj as the first @ Pointcut (value = POINT_CUT) public void pointCut (EsLog esLog) {} / / same as pointCut. ArgNames can remove @ Around (value = "pointCut (esLog)", argNames = "pj, esLog") public Object around (ProceedingJoinPoint pj, EsLog esLog) throws Throwable {StopWatch sw = StopWatch.createStarted (); EsLogDto dto = null Try {Object obj = pj.proceed (); if (esLog.available ()) {dto = wrap (pj.getArgs (), esLog, obj);} return obj;} catch (Throwable ex) {if (esLog.available ()) {dto = wrap (pj.getArgs (), esLog, ex) } throw ex;} finally {sw.stop (); if (dto! = null) {dto.setCostTime (sw.getTime (TimeUnit.MILLISECONDS)); dto.setAddTime (new Date ()); LOGGER.info (dto.toString ()) } private EsLogDto wrap (Object [] args, EsLog esLog, Object... Objs) {EsLogDto eld = new EsLogDto (); switch (esLog.source ()) {case XFXJ: {String apiCode = (String) args [0]; eld.setApiCode (apiCode); String apiParam = (String) args [1]; eld.setReq (apiParam); JSONObject obJson = JSON.parseObject (apiParam) String id = obJson.getString ("applySerialNo"); if (id! = null) {eld.setId (id);} else {/ / standby snowflake algorithm eld.setId (SnowFlake.getInstance (). NextId ());} Object obj = objs [0] If (obj instanceof CustomException) {/ / Custom exception CustomException ce = (CustomException) obj; eld.setErrorCode (ce.getErrorCode ()); eld.setErrorMsg (ce.getMessage ());} else if (obj instanceof Exception) {/ / Runtime exception Exception ex = (Exception) obj Eld.setErrorCode (HttpStatus.INTERNAL_SERVER_ERROR.value ()); eld.setErrorMsg (ex.getMessage ());} else {/ / No exception eld.setRes (obj.toString ());}} break Case H5://TODO to be extended default: break;} return eld;}}

6. For the following errors:

IllegalArgumentException: error at:: 0 formal unbound in pointcut

Error creating bean with name 'tomcatServletWebServerFactory

After reading the above, have you mastered the methods of @ Pointcut and @ Around in springboot aspect? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report