<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          面試官:Spring 注解 @After,@Around,@Before 的執(zhí)行順序是?

          共 5808字,需瀏覽 12分鐘

           ·

          2021-11-15 05:04

          點(diǎn)擊關(guān)注公眾號(hào),Java干貨及時(shí)送達(dá)

          AOP中有@Before@After@Around@AfterRunning注解等等。

          ");????}?????@AfterThrowing("within(com.lmx.blog.controller.*Controller)")????public?void?afterThrowing(){????????System.out.println("異常出現(xiàn)之后...afterThrowing");????}??}@Before,@After,@Around注解的區(qū)別大家可以自行百度下。" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">首先上下自己的代碼,定義了切點(diǎn)的定義

          ");????}?????@AfterThrowing("within(com.lmx.blog.controller.*Controller)")????public?void?afterThrowing(){????????System.out.println("異常出現(xiàn)之后...afterThrowing");????}??}@Before,@After,@Around注解的區(qū)別大家可以自行百度下。" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">@Aspect
          @Component
          public?class?LogApsect?{
          ?
          ????private?static?final?Logger?logger?=?LoggerFactory.getLogger(LogApsect.class);
          ?
          ????ThreadLocal?startTime?=?new?ThreadLocal<>();
          ?
          ????//?第一個(gè)*代表返回類型不限
          ????//?第二個(gè)*代表所有類
          ????//?第三個(gè)*代表所有方法
          ????//?(..)?代表參數(shù)不限
          ????@Pointcut("execution(public?*?com.lmx.blog.controller.*.*(..))")
          ????@Order(2)
          ????public?void?pointCut(){};
          ?
          ????@Pointcut("@annotation(com.lmx.blog.annotation.RedisCache)")
          ????@Order(1)?//?Order?代表優(yōu)先級(jí),數(shù)字越小優(yōu)先級(jí)越高
          ????public?void?annoationPoint(){};
          ?
          ????@Before(value?=?"annoationPoint()?||?pointCut()")
          ????public?void?before(JoinPoint?joinPoint){
          ????????System.out.println("方法執(zhí)行前執(zhí)行......before");
          ????????ServletRequestAttributes?attributes?=?(ServletRequestAttributes)?RequestContextHolder.getRequestAttributes();
          ????????HttpServletRequest?request?=?attributes.getRequest();
          ????????logger.info("<=====================================================");
          ????????logger.info("請(qǐng)求來源:?=》"?+?request.getRemoteAddr());
          ????????logger.info("請(qǐng)求URL:"?+?request.getRequestURL().toString());
          ????????logger.info("請(qǐng)求方式:"?+?request.getMethod());
          ????????logger.info("響應(yīng)方法:"?+?joinPoint.getSignature().getDeclaringTypeName()?+?"."?+?joinPoint.getSignature().getName());
          ????????logger.info("請(qǐng)求參數(shù):"?+?Arrays.toString(joinPoint.getArgs()));
          ????????logger.info("------------------------------------------------------");
          ????????startTime.set(System.currentTimeMillis());
          ????}
          ?
          ????//?定義需要匹配的切點(diǎn)表達(dá)式,同時(shí)需要匹配參數(shù)
          ????@Around("pointCut()?&&?args(arg)")
          ????public?Response?around(ProceedingJoinPoint?pjp,String?arg)?throws?Throwable{
          ????????System.out.println("name:"?+?arg);
          ????????System.out.println("方法環(huán)繞start...around");
          ????????String?result?=?null;
          ????????try{
          ????????????result?=?pjp.proceed().toString()?+?"aop?String";
          ????????????System.out.println(result);
          ????????}catch?(Throwable?e){
          ????????????e.printStackTrace();
          ????????}
          ????????System.out.println("方法環(huán)繞end...around");
          ????????return?(Response)?pjp.proceed();
          ????}
          ?
          ????@After("within(com.lmx.blog.controller.*Controller)")
          ????public?void?after(){
          ????????System.out.println("方法之后執(zhí)行...after.");
          ????}
          ?
          ????@AfterReturning(pointcut="pointCut()",returning?=?"rst")
          ????public?void?afterRunning(Response?rst){
          ????????if(startTime.get()?==?null){
          ????????????startTime.set(System.currentTimeMillis());
          ????????}
          ????????System.out.println("方法執(zhí)行完執(zhí)行...afterRunning");
          ????????logger.info("耗時(shí)(毫秒):"?+??(System.currentTimeMillis()?-?startTime.get()));
          ????????logger.info("返回?cái)?shù)據(jù):{}",?rst);
          ????????logger.info("==========================================>");
          ????}
          ?
          ????@AfterThrowing("within(com.lmx.blog.controller.*Controller)")
          ????public?void?afterThrowing(){
          ????????System.out.println("異常出現(xiàn)之后...afterThrowing");
          ????}
          ?
          ?
          }

          ");????}?????@AfterThrowing("within(com.lmx.blog.controller.*Controller)")????public?void?afterThrowing(){????????System.out.println("異常出現(xiàn)之后...afterThrowing");????}??}@Before,@After,@Around注解的區(qū)別大家可以自行百度下。" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">@Before@After@Around注解的區(qū)別大家可以自行百度下。

          總之就是@Around可以實(shí)現(xiàn)@Before@After的功能,并且只需要在一個(gè)方法中就可以實(shí)現(xiàn)。推薦一個(gè) Spring Boot 基礎(chǔ)教程及實(shí)戰(zhàn)示例:https://github.com/javastacks/spring-boot-best-practice

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">首先我們來測(cè)試一個(gè)方法用于獲取數(shù)據(jù)庫一條記錄的

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO  c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO  c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">@RequestMapping("/achieve")
          public?Response?achieve(){
          ????System.out.println("方法執(zhí)行-----------");
          ????return?Response.ok(articleDetailSercice.getPrimaryKeyById(1L));
          }

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">以下是控制臺(tái)打印的日志

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO  c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO  c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">方法執(zhí)行前執(zhí)行......before
          2018-11-23?16:31:59.795?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?<=====================================================
          2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:1
          2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/achieve
          2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET
          2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.achieve
          2018-11-23 16:31:59.796 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[]
          2018-11-23?16:31:59.796?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------
          方法執(zhí)行-----------
          2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???
          2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???
          2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)
          2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)
          2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1
          2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1
          方法之后執(zhí)行...after.
          方法執(zhí)行完執(zhí)行...afterRunning
          2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):27
          2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce5
          2018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before@after方法,不符合@Around的匹配邏輯)

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO  c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO  c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">@RedisCache(type?=?Response.class)
          @RequestMapping("/sendEmail")
          public?Response?sendEmailToAuthor(String?content){
          ????System.out.println("測(cè)試執(zhí)行次數(shù)");
          ????return?Response.ok(true);
          }

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">以下是該部分代碼的console打印

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO  c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO  c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO  c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">name:第二封郵件呢
          方法環(huán)繞start...around
          方法執(zhí)行前執(zhí)行......before
          2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================
          2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:1
          2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail
          2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET
          2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor
          2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]
          2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------
          測(cè)試執(zhí)行次數(shù)
          com.lmx.blog.common.Response@6d17f2fdaop?String
          方法環(huán)繞end...around
          方法執(zhí)行前執(zhí)行......before
          2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================
          2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:1
          2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail
          2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET
          2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor
          2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]
          2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------
          測(cè)試執(zhí)行次數(shù)
          方法之后執(zhí)行...after.
          方法執(zhí)行完執(zhí)行...afterRunning
          2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):0
          2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f85428
          2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>

          ??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>??Preparing:?select?*?from?article_detail?where?id?=???2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.806?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?==>?Parameters:?1(Long)2018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?12018-11-23?16:31:59.814?[http-nio-8888-exec-9]?DEBUG?c.l.b.m.A.selectPrimaryKey?-?<==??????Total:?1方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):272018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@8675ce52018-11-23?16:31:59.824?[http-nio-8888-exec-9]?INFO??c.l.blog.config.LogApsect?-?==========================================>可以看到,因?yàn)闆]有匹配@Around的規(guī)則,所以沒有進(jìn)行環(huán)繞通知。(PS:我定義的環(huán)繞通知意思是要符合是 controller 包下的方法并且方法必須帶有參數(shù),而上述方法沒有參數(shù),所以只走了@before和@after方法,不符合@Around的匹配邏輯)我們?cè)僭囈幌铝硪粋€(gè)帶有參數(shù)的方法@RedisCache(type?=?Response.class)@RequestMapping("/sendEmail")public?Response?sendEmailToAuthor(String?content){????System.out.println("測(cè)試執(zhí)行次數(shù)");????return?Response.ok(true);}以下是該部分代碼的console打印name:第二封郵件呢方法環(huán)繞start...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.347?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.348?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)com.lmx.blog.common.Response@6d17f2fdaop?String方法環(huán)繞end...around方法執(zhí)行前執(zhí)行......before2018-11-23?16:34:55.349?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?<=====================================================2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:12018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]2018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------測(cè)試執(zhí)行次數(shù)方法之后執(zhí)行...after.方法執(zhí)行完執(zhí)行...afterRunning2018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):02018-11-23 16:34:55.350?[http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@79f854282018-11-23?16:34:55.350?[http-nio-8888-exec-2]?INFO??c.l.blog.config.LogApsect?-?==========================================>顯而易見," linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" style="color: rgb(58, 58, 58);" data-linktype="2">顯而易見,該方法符合 @Around 環(huán)繞通知的匹配規(guī)則,所以進(jìn)入了@Around的邏輯,但是發(fā)現(xiàn)了問題,所有的方法都被執(zhí)行了2次,不管是切面層還是方法層。(有人估計(jì)要問我不是用的自定義注解 @RedisCache(type = Response.class) 么。為什么會(huì)符合 @Around的匹配規(guī)則呢,這個(gè)等會(huì)在下面說)

          點(diǎn)擊關(guān)注公眾號(hào),Java干貨及時(shí)送達(dá)

          我們分析日志的打印順序可以得出,在執(zhí)行環(huán)繞方法時(shí)候,會(huì)優(yōu)先進(jìn)入 @Around下的方法。@Around的方法再貼一下代碼。

          //?定義需要匹配的切點(diǎn)表達(dá)式,同時(shí)需要匹配參數(shù)
          @Around("pointCut()?&&?args(arg)")
          public?Response?around(ProceedingJoinPoint?pjp,String?arg)?throws?Throwable{
          ????System.out.println("name:"?+?arg);
          ????System.out.println("方法環(huán)繞start...around");
          ????String?result?=?null;
          ????try{
          ????????result?=?pjp.proceed().toString()?+?"aop?String";
          ????????System.out.println(result);
          ????}catch?(Throwable?e){
          ????????e.printStackTrace();
          ????}
          ????System.out.println("方法環(huán)繞end...around");
          ????return?(Response)?pjp.proceed();
          }

          打印了前兩行代碼以后,轉(zhuǎn)而去執(zhí)行了 @Before方法,是因?yàn)橹型居|發(fā)了 ?ProceedingJoinPoint.proceed() 方法。

          這個(gè)方法的作用是執(zhí)行被代理的方法,也就是說執(zhí)行了這個(gè)方法之后會(huì)執(zhí)行我們controller的方法,而后執(zhí)行 @before@after,然后回到@Around執(zhí)行未執(zhí)行的方法,最后執(zhí)行 @afterRunning,如果有異常拋出能執(zhí)行 @AfterThrowing

          也就是說環(huán)繞的執(zhí)行順序是 ? @Around→@Before→@After→@Around執(zhí)行 ProceedingJoinPoint.proceed() 之后的操作→@AfterRunning(如果有異常→@AfterThrowing)

          而我們上述的日志相當(dāng)于把上述結(jié)果執(zhí)行了2遍,根本原因在于 ProceedingJoinPoint.proceed() 這個(gè)方法,可以發(fā)現(xiàn)在@Around 方法中我們使用了2次這個(gè)方法,然而每次調(diào)用這個(gè)方法時(shí)都會(huì)走一次@Before→@After→@Around執(zhí)行 ProceedingJoinPoint.proceed() 之后的操作→@AfterRunning(如果有異常→@AfterThrowing)。

          回到上述未解決的問題,為什么我定義了切面的另一個(gè)注解還可以進(jìn)入@Around方法呢?" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">因此問題是出現(xiàn)在這里。所以更改@Around部分的代碼即可解決該問題。更改之后的代碼如下:

          回到上述未解決的問題,為什么我定義了切面的另一個(gè)注解還可以進(jìn)入@Around方法呢?" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">@Around("pointCut()?&&?args(arg)")
          public?Response?around(ProceedingJoinPoint?pjp,String?arg)?throws?Throwable{
          ????System.out.println("name:"?+?arg);
          ????System.out.println("方法環(huán)繞start...around");
          ????String?result?=?null;
          ????Object?object?=?pjp.proceed();
          ????try{
          ????????result?=?object.toString()?+?"aop?String";
          ????????System.out.println(result);
          ????}catch?(Throwable?e){
          ????????e.printStackTrace();
          ????}
          ????System.out.println("方法環(huán)繞end...around");
          ????return?(Response)?object;
          }

          回到上述未解決的問題,為什么我定義了切面的另一個(gè)注解還可以進(jìn)入@Around方法呢?" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" style="color: rgb(58, 58, 58);" data-linktype="2">更改代碼之后的運(yùn)行結(jié)果

          回到上述未解決的問題,為什么我定義了切面的另一個(gè)注解還可以進(jìn)入@Around方法呢?" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">name:第二封郵件呢
          方法環(huán)繞start...around
          方法執(zhí)行前執(zhí)行......before
          2018-11-23?16:52:14.315?[http-nio-8888-exec-4]?INFO??c.l.blog.config.LogApsect?-?<=====================================================
          2018-11-23 16:52:14.315 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect -?請(qǐng)求來源:?=》0:0:0:0:0:0:0:1
          2018-11-23 16:52:14.315 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect -?請(qǐng)求URL:http://localhost:8888/user/sendEmail
          2018-11-23 16:52:14.315 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect -?請(qǐng)求方式:GET
          2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect -?響應(yīng)方法:com.lmx.blog.controller.UserController.sendEmailToAuthor
          2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect -?請(qǐng)求參數(shù):[第二封郵件呢]
          2018-11-23?16:52:14.316?[http-nio-8888-exec-4]?INFO??c.l.blog.config.LogApsect?-?------------------------------------------------------
          測(cè)試執(zhí)行次數(shù)
          com.lmx.blog.common.Response@1b1c76afaop?String
          方法環(huán)繞end...around
          方法之后執(zhí)行...after.
          方法執(zhí)行完執(zhí)行...afterRunning
          2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect -?耗時(shí)(毫秒):0
          2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect -?返回?cái)?shù)據(jù):com.lmx.blog.common.Response@1b1c76af
          2018-11-23?16:52:14.316?[http-nio-8888-exec-4]?INFO??c.l.blog.config.LogApsect?-?==========================================>

          回到上述未解決的問題,為什么我定義了切面的另一個(gè)注解還可以進(jìn)入@Around方法呢?" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">回到上述未解決的問題,為什么我定義了切面的另一個(gè)注解還可以進(jìn)入@Around方法呢?

          因?yàn)槲覀兊姆椒ㄈ匀辉赾ontroller下,因此滿足該需求,如果我們定義了controller包下的某個(gè)controller才有用。最新面試題整理好了,點(diǎn)擊Java面試庫小程序在線刷題。

          例如:

          @Pointcut("execution(public?*?com.lmx.blog.controller.UserController.*(..))")

          而如果我們剛才定義的方法是寫在 TestController 之下的,那么就不符合 @Around方法的匹配規(guī)則了,也不符合@before@after的注解規(guī)則,因此不會(huì)匹配任何一個(gè)規(guī)則,如果需要匹配特定的方法,可以用自定義的注解形式或者特性controller下的方法

          ①:特性的注解形式

          @Pointcut("@annotation(com.lmx.blog.annotation.RedisCache)")
          @Order(1)?//?Order?代表優(yōu)先級(jí),數(shù)字越小優(yōu)先級(jí)越高
          public?void?annoationPoint(){};

          然后在所需要的方法上加入 @RedisCache注解,在@Before@After@Around等方法上添加該切點(diǎn)的方法名(“annoationPoint()”),如果有多個(gè)注解需要匹配則用 || 隔開

          ②:指定controller或者指定controller下的方法

          @Pointcut("execution(public?*?com.lmx.blog.controller.UserController.*(..))")
          @Order(2)
          public?void?pointCut(){};

          該部分代碼是指定了 com.lmx.blog.controller包下的UserController下的所有方法。另外,Spring 系列面試題和答案全部整理好了,微信搜索Java技術(shù)棧,在后臺(tái)發(fā)送:面試,可以在線閱讀。

          第一個(gè)*代表的是返回類型不限

          第二個(gè)*代表的是該controller下的所有方法,(..)代表的是參數(shù)不限

          總結(jié)

          當(dāng)方法符合切點(diǎn)規(guī)則不符合環(huán)繞通知的規(guī)則時(shí)候,執(zhí)行的順序如下

          @Before→@After→@AfterRunning(如果有異常→@AfterThrowing)

          當(dāng)方法符合切點(diǎn)規(guī)則并且符合環(huán)繞通知的規(guī)則時(shí)候,執(zhí)行的順序如下

          @Around→@Before→@Around→@After執(zhí)行 ProceedingJoinPoint.proceed() 之后的操作→@AfterRunning(如果有異常→@AfterThrowing)

          原文鏈接:https://blog.csdn.net/lmx125254/article/details/84398412

          版權(quán)聲明:本文為CSDN博主「Leonis丶L」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。





          關(guān)注Java技術(shù)棧看更多干貨



          獲取 Spring Boot 實(shí)戰(zhàn)筆記!
          瀏覽 47
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  在线观看免费无码视频 | 免费网站观看www在线观看 | 亚洲AV免费网站在线观看 | 一区二区三区四区无码在线 | 黄色a电影 |