Spring AOP 看不到方法
Spring AOP doesn't see the method
在我的项目中,我有 AspectService class,我用它来记录所有控制器方法:
@Component
@SLFJ
@Aspect
public class AspectService {
@Pointcut("@annotation(com.aleksandr0412.bookstore.annotations.Audit) && execution(public * *(..))")
public void publicAspectAudit() {
}
@Around(value = "publicAspectAudit()")
public Object aspect(ProceedingJoinPoint joinPoint) throws Throwable {
ObjectMapper om = new ObjectMapper();
AuditMessage auditMessage = new AuditMessage();
UUID uuid = UUID.randomUUID();
auditMessage.setUuid(uuid);
auditMessage.setAuditCode(((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(Audit.class).value());
auditMessage.setEventCode(EventCode.START);
auditMessage.setTimeStart(LocalDateTime.now());
auditMessage.setUsername("");
Object[] args = Arrays.stream(joinPoint.getArgs())
.filter(arg -> !(arg instanceof UriComponentsBuilder)).toArray();
auditMessage.setParams(om.writeValueAsString(args));
log.info(auditMessage.toString());
//-----
我的项目有 3 个模块:第一个是审计和 AspectService,第二个和第三个是具有不同控制器的可执行模块。
我的问题是在一个模块中 public 带有 @Audit 注释的控制器方法是正确的,但在另一个模块中,AspectService 没有看不到他们。
工作正确:
@Audit(AuditCode.AUTHOR_CREATE)
@Override
public ResponseEntity<AuthorDto> createAuthor(AuthorDto authorDto, UriComponentsBuilder componentsBuilder) {
log.info("createAuthor with {} - start ", authorDto);
AuthorDto result = service.addAuthor(authorDto);
URI uri = componentsBuilder.path("/api/author/" + result.getId()).buildAndExpand(result).toUri();
log.info("createAuthor end with {}, with result {}", authorDto, result);
return ResponseEntity.created(uri).body(result);
}
无效:
@Audit(AuditCode.CREATE_USER)
@Override
public ResponseEntity<UserDTO> createUser(
UserDTO userDTO,
UriComponentsBuilder componentsBuilder
) {
log.info("createUser with {} - start ", userDTO);
if (userDTO == null) {
throw new EmptyException();
}
UserDTO result = userService.add(userDTO);
URI uri = componentsBuilder.path("/api/user/" + result.getId()).buildAndExpand(result).toUri();
log.info("createUser end with result {}", result);
return ResponseEntity.created(uri).body(result);
}
当我尝试调试它时,在第二个示例中我没有访问 AspectService class。
我哪里出错了?
我将带有 AspectService 包的 @ComponentScan 添加到第二个模块。它解决了我的问题
在我的项目中,我有 AspectService class,我用它来记录所有控制器方法:
@Component
@SLFJ
@Aspect
public class AspectService {
@Pointcut("@annotation(com.aleksandr0412.bookstore.annotations.Audit) && execution(public * *(..))")
public void publicAspectAudit() {
}
@Around(value = "publicAspectAudit()")
public Object aspect(ProceedingJoinPoint joinPoint) throws Throwable {
ObjectMapper om = new ObjectMapper();
AuditMessage auditMessage = new AuditMessage();
UUID uuid = UUID.randomUUID();
auditMessage.setUuid(uuid);
auditMessage.setAuditCode(((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(Audit.class).value());
auditMessage.setEventCode(EventCode.START);
auditMessage.setTimeStart(LocalDateTime.now());
auditMessage.setUsername("");
Object[] args = Arrays.stream(joinPoint.getArgs())
.filter(arg -> !(arg instanceof UriComponentsBuilder)).toArray();
auditMessage.setParams(om.writeValueAsString(args));
log.info(auditMessage.toString());
//-----
我的项目有 3 个模块:第一个是审计和 AspectService,第二个和第三个是具有不同控制器的可执行模块。 我的问题是在一个模块中 public 带有 @Audit 注释的控制器方法是正确的,但在另一个模块中,AspectService 没有看不到他们。
工作正确:
@Audit(AuditCode.AUTHOR_CREATE)
@Override
public ResponseEntity<AuthorDto> createAuthor(AuthorDto authorDto, UriComponentsBuilder componentsBuilder) {
log.info("createAuthor with {} - start ", authorDto);
AuthorDto result = service.addAuthor(authorDto);
URI uri = componentsBuilder.path("/api/author/" + result.getId()).buildAndExpand(result).toUri();
log.info("createAuthor end with {}, with result {}", authorDto, result);
return ResponseEntity.created(uri).body(result);
}
无效:
@Audit(AuditCode.CREATE_USER)
@Override
public ResponseEntity<UserDTO> createUser(
UserDTO userDTO,
UriComponentsBuilder componentsBuilder
) {
log.info("createUser with {} - start ", userDTO);
if (userDTO == null) {
throw new EmptyException();
}
UserDTO result = userService.add(userDTO);
URI uri = componentsBuilder.path("/api/user/" + result.getId()).buildAndExpand(result).toUri();
log.info("createUser end with result {}", result);
return ResponseEntity.created(uri).body(result);
}
当我尝试调试它时,在第二个示例中我没有访问 AspectService class。 我哪里出错了?
我将带有 AspectService 包的 @ComponentScan 添加到第二个模块。它解决了我的问题