面向切面:所有业务都要处理的业务。 例如:日志服务
1.在使用AOP之前,在pom.xml文件中导入相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.编写一个Aspect实现在Logger功能:
package com.example.demo.aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Before("execution(* com.example.demo.controller.Index.*(..))")
public void BeforeAccessCheck() {
logger.info("before method");
}
@After("execution(* com.example.demo.