Spring boot 結合aop,自定義註解,切面的理解
阿新 • • 發佈:2018-12-09
package com.imooc.boot.controller; import com.imooc.boot.aspect.IndexAspect; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("member") public class MemberController { @IndexAspect @RequestMapping(value = "/index",method = RequestMethod.GET) public String index(){ System.out.println("aspect test after"); return "SUCCESS"; } } package com.imooc.boot.aspect; import java.lang.annotation.*; /** * 自定義的註解 */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface IndexAspect { } package com.imooc.boot.aspect; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * 定義切面類 */ @org.aspectj.lang.annotation.Aspect @Component public class Aspect { /** * 定義切點-->自定義註解 */ @Pointcut("@annotation(com.imooc.boot.aspect.IndexAspect)") public void afterAsperct(){ } /** * 具體在切點前面前或者後需要執行的流程, * 在此只寫了在切點前執行 * -->切點後執行的話 @After 相關的註解, */ @Before("afterAsperct()") public void before(){ System.out.println("切面前執行-->"); } }