Spring 引导应用程序中未触发方面

Aspect is not being triggered in Spring Boot application

我正在使用 Spring Boot 开发一个应用程序,我希望它的所有日志记录都通过 Aspects,但我无法使其工作。 我用 @AfterThrowing@Before 设置了 2 个方法,最后一个作为测试。切入点正在检查 Controller、Services 和 Repositories 的包。 问题是日志和 System.out.println() 都没有显示在控制台中。

我已经尝试了在这里看到的不同解决方案,但其中 none 对我有用。我有正确的依赖项,不需要进一步的配置,因为它已经通过 Spring Boot 完成,我什至尝试使用带有 类.

注释的切入点

这是我的包层次结构

Spring 家属

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

LoggingAspect

package com.tlc.tracker.global.logging;

import com.tlc.tracker.global.exception.BusinessServiceException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
@Slf4j
public class LoggingAspect {

@Pointcut("execution(* com.tlc.tracker.v01.controller.*.*(..))")
public void controllerMethods() { }

@Pointcut("execution(* com.tlc.tracker.v01.service.imp.ProjectServiceImpl.*(..))")
public void serviceMethods(){  }

@Pointcut("execution(* com.tlc.tracker.v01.repository.*.*(..))")
public void respositoryMethods(){  }

@Before("serviceMethods()")
public void Areturn(JoinPoint point){
    System.out.println("Hola");
    log.info("HOLA");
}

@AfterThrowing(pointcut = "controllerMethods() && serviceMethods() && respositoryMethods()", throwing = "exception")
public void logAfterThrowing(JoinPoint joinPoint, Throwable exception){

    if(exception instanceof BusinessServiceException){
        //Get the parts of the exception message
        String exceptions[] = exception.getMessage().split(":", 2);
        //Get the message contained in the exception message
        String message = exceptions.length == 1 ? "" : exceptions[1];

        log.error("[" + joinPoint.getSignature().getName() + "] - Type: " + exceptions[0] + ". Message: "+ message);
    }

    if(exception instanceof Exception){
        log.error("[" + joinPoint.getSignature().getName() + "] - Type: ServerError. Message: "  + exception.getMessage());
    }
}

}

项目控制器

@RestController
@RequestMapping("/v01")
@Slf4j
public class ProjectController {

    @Autowired
    private ProjectServiceIface projectService;

    @PostMapping(path = "/project", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Project> createProject(@Valid @RequestBody Project project, BindingResult result){
        if(result.hasErrors()){
            throw new BusinessServiceException(result.getFieldError().getDefaultMessage(), result.getFieldError().getField() + " " + result.getFieldError().getCode());
        }
        Project projectSaved = projectService.createProject(project);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Location", projectSaved.getId().toString());

        return new ResponseEntity<>(project, headers, HttpStatus.CREATED);
    }
}

项目服务

@Service
public class ProjectServiceImpl implements ProjectServiceIface {

    @Autowired
    private ProjectRepository projectRepository;

    @Override
    public Project createProject(Project project) {
        Project projectFound = projectRepository.findByName(project.getName());
        if(projectFound == null){
            throw new BusinessServiceException(Constants.FUNCTIONAL_ERROR, "The Project already exists");
        }
        project.setCreateTime(new Date());

        Project projectSaved = projectRepository.save(project);
        return projectSaved;
    }
}

项目资源库

public interface ProjectRepository extends CrudRepository<Project, Integer> {

    @Query(value = "SELECT * FROM Project p WHERE p.name = :projectName", nativeQuery = true)
    public Project findByName(String projectName);
}

我的猜测是你的 LoggingAspect class 在组件扫描期间没有被拾取,因为它没有在 JavaConfigXmlConfig 中明确声明,并且没有@Component 注释(并且 @Aspect 注释本身没有此元注释)。

此外,@AfterThrowing 建议包括所有使用 && 而不是 || 的命名切入点,声明所有切入点必须匹配(这是不可能的,因为它们都引用不同的包)。