SpringBoot——面向切面编程AOP
SpringBoot中使用AOP,和以前Spring中使用AOP没什么区别。如果你连AOP是什么都不清楚,请先看这篇博客:https://blog.csdn.net/u010837612/article/details/45583581
在SpringBoot中使用AOP,导入的依赖包不太一样:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
具体使用方法都和Spring中一样的,具体可以看上面给出的链接。这里也给出一个简单的例子。
先写一个Controller,输出hello
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
System.out.println("hello");
return "";
}
}
然后写一个切面类:
package com.example.demo.ascept;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
@Before("execution(* com.example.demo.controller.*.*(..))")
public void before(){
System.out.println("before......");
}
@After("execution(* com.example.demo.controller.*.*(..))")
public void after(){
System.out.println("after......");
}
}
运行,然后访问localhost:8080/hello,查看控制台
还没有评论,来说两句吧...