SpringBoot自定义starter
- SpringBoot与AOP
SpringBoot与AOP
使用AOP实现用户接口访问日志功能
- 添加AOP场景启动器
<!--添加AOP场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
- 定义切面类
@Aspect
@Component
public class LogAspect {
Logger log= LoggerFactory.getLogger(LogAspect.class);
@Around("execution(* com.springboot.controller.*.*(..))&&" +
"@annotation(apiOperation)")
public Object around(ProceedingJoinPoint joinPoint, ApiOperation apiOperation) throws Throwable {
StringBuilder loginfo=new StringBuilder();
Class<?> controller = joinPoint.getThis().getClass();
Api annotation = controller.getAnnotation(Api.class);
if (annotation != null) {
loginfo.append(annotation.value());
}
String value = apiOperation.value();
loginfo.append(value);
log.info("请求接口相关信息:{}",loginfo.toString());
return joinPoint.proceed();
}
}
- 测试
通过swaager进行测试
可以看到日志信息已经被打印