In my project I have AspectService class, that I use to log all controller methods:
@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());
//-----
My project has 3 modules: first - audit and AspectService, second and third is executable modules with different controllers. My problem is that in one module public controller-methods with @Audit annotations work true, but in another module, AspectService doesn't see them.
Work true:
@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);
}
Doesn't work:
@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);
}
When I try to debug it, in second example I don't visit AspectService class. Where am I make mistake?
I add @ComponentScan with AspectService package to second module. It solved my problem