我正在尝试使用自定义注释和Spring AOP创建一个Spring Boot库。当我将此库与新的spring boot应用程序一起使用时。然后它不起作用。甚至我也没有任何错误。
图书馆样品-
自定义注释
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface HttpLogger {
}
春季AOP班
@Aspect
class LoggingAspect {
@Around("@annotation(com.demo.commonlogging.aspect.HttpLogger)")
public Object inControllers(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
return loggingAdvice(proceedingJoinPoint); // Method for implementation
}
}
pom.xml
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
使用mvn全新安装来创建库
现在,新库已导入springboot应用程序中。
并且控制器中使用了新的自定义注释
控制器
@RestController
@RequestMapping(value = "/rest/test")
public class RestApiTestControllers {
@GetMapping
@HttpLogger
public String get(){
return "Hello !";
}
}
请在这里帮助。
答案 0 :(得分:0)
似乎您从@Component
中丢失了LoggingAspect
,也会打电话继续proceedingJoinPoint.proceed();
并返回其值。
因此您的代码应如下所示:
@Aspect
@Component
class LoggingAspect {
@Around("@annotation(com.demo.commonlogging.aspect.HttpLogger)")
public Object inControllers(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("Before call");
Object returned = proceedingJoinPoint.proceed();
System.out.println("After call");
return returned;
}
}
希望这会有所帮助!