SpringAOP没有看到这个方法

nkkqxpd9  于 2021-07-22  发布在  Java
关注(0)|答案(1)|浏览(305)

在我的项目中,我有aspectservice类,用于记录所有控制器方法:

@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,第二个和第三个是具有不同控制器的可执行模块。我的问题是,在一个模块中,带有@audit注解的public controller方法工作正常,但在另一个模块中,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类。我哪里出错了?

u0njafvf

u0njafvf1#

我将@componentscan和aspectservice包添加到第二个模块中。它解决了我的问题

相关问题