gpt4 book ai didi

java - Spring Boot 应用程序中未触发方面

转载 作者:行者123 更新时间:2023-11-30 05:20:09 27 4
gpt4 key购买 nike

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

我尝试了在这里看到的不同解决方案,但没有一个对我有用。我有正确的依赖项,不需要进一步的配置,因为它已经使用 Spring Boot 完成,我什至尝试将切入点与类的注释一起使用。

这是我的包层次结构

Packages

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>

日志记录方面

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());
}
}

}

项目 Controller

@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 类在组件扫描期间没有被拾取,因为它没有在 JavaConfigXmlConfig 中显式声明,并且没有 @Component 注释(并且 @Aspect 注释本身没有此元注释)。

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

关于java - Spring Boot 应用程序中未触发方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59707561/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com