前言:AOP:将通用逻辑从业务逻辑中分离出来
一、在pom.xml中添加AOP依赖
<!--添加aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
二、往常的做法是在启动类上添加注解,但是aop不需要
三、建立我们的处理文件,首先建一个aspect包,再在包下面建议我们的类,
然后在类上面我们添加@Aspect注解和@Component注解如下:
/**
* 系统日志,处理切面类
*/
@Aspect
@Component
public class HttpAspect {
}
四、我们现在要做的是在做一些增删改查前要验证是否登录了,下面我们就在HttpAspect下面进行验证和拦截。
@Aspect
@Component
public class HttpAspect {
@Before("execution(public * com.imooc.controller.GirlController.girlList(..))")
public void log(){
System.out.println(11111);
}
}
这里我们先验证一下girlList方法,格式就如上面的代码一样,下面我们运行程序进行测试!
这里我们还是用postman进行接口测试,看一下控制台: