弄一个简单的Spring AOP的demo,如下
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestController { @RequestMapping("hello") public String HelloWorld(){ return "Hello world"; } }
@Aspect @Component public class LogAspect { @Around("execution(* com.wangc.demo.controller.*.*(..))") public void aroundTest(JoinPoint joinPoint){ System.out.println("执行了aroundTest()方法"); } @Before("execution(* com.wangc.demo.controller.*.*(..))") public void beforeTest(JoinPoint joinPoint){ System.out.println("执行了beforeTest()方法"); } @After("execution(* com.wangc.demo.controller.*.*(..))") public void afterTest(JoinPoint joinPoint){ System.out.println("执行了afterTest()方法"); } }
很简单的一个Aspect切面类,但是运行结果却是这样的
执行了aroundTest()方法
执行了afterTest()方法
调用的“hello”接口也没有运行,很奇怪,麻烦哪位大佬帮忙看一下,不甚感激!